Disabling a field when editing an existing record in a Datagrid

  • Thread starter Thread starter Mark Perona
  • Start date Start date
M

Mark Perona

I created an ASP.net form with an editable datagrid on it. I can
create new records, and update and delete existing records. The
problem I have is that I want a field in the grid to be editible when
I'm editing a new record, but disabled when editable an existing
record. Any ideas

Mark
 
try it it should work
<asp:TemplateColumn HeaderText=" Comments">
<HeaderStyle HorizontalAlign="Center" Width="130px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblNote" text='<%#Container.DataItem("Note")%>'
runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNote" Runat="server" ReadOnly="True"
text='<%#Container.DataItem("Note")%>' Rows="2" TextMode="MultiLine"
MaxLength="250">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
 
Where do you display the fields to add a new record ?

You cannot edit a new record, you add it, then it becomes
an existing record, and then you Edit the existing
record...

So I am assuming that you are using a DataSet, Adding the
record to the DataTable before you send it to the
database, displaying in the DataGrid and then Commiting it
to the database when the user confirms.

in this case you can check the RowState of the currentrow
of the DataTable to check if it is a new record or not.
LEts say you have a DataKeyField ID in dtNotes datatable...
You can put in the codebehind

protected DataSet dsNotes;
protected DataTable dtNotes ;

public bool CheckNew(object myKey)
{

}

<asp:TextBox ID="txtNote" Runat="server" ReadOnly='<%
#CheckNew(Container.DataItem("ID"))%>'
text='<%#Container.DataItem("Note")%>' Rows="2"
TextMode="MultiLine"
MaxLength="250">
 
Back
Top