0

I'm binding an array to GridView. Below is my template field and it doesn't fire RowUpdating when I click on update.

<asp:TemplateField HeaderText="Role">
                <EditItemTemplate>
                    <asp:TextBox runat="server" Text='<%# Container.DataItem.ToString() %>' ID="txtEditRole"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <%# Container.DataItem.ToString() %>
                </ItemTemplate>
            </asp:TemplateField>

This happened after making the field to TempleteField. Earlier the field was like below.

<asp:BoundField DataField="!" HeaderText="Role" />
user966398
  • 59
  • 3
  • 12

1 Answers1

0

Make sure you specify OnRowUpdating="gv_RowUpdating" event and also change FieldName in <%#Eval("Role") %>, see this example:

.aspx page

<asp:GridView ID="gv" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" OnRowEditing="gv_RowEditing"      
OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowCommand="gv_RowCommand" OnRowDeleting="gv_RowDeleting">
<Columns>
   <asp:TemplateField>
     <EditItemTemplate>
       <asp:LinkButton ID="lbtnUpdate" runat="server" CommandName="Update" Text="Update" />
       <asp:LinkButton ID="lbtnCancel" runat="server" CommandName="Cancel" Text="Cancel" />
     </EditItemTemplate>
     <ItemTemplate>
        <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit" />
        <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" CausesValidation="false" />
     </ItemTemplate>    
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Role">
     <EditItemTemplate>
       <asp:TextBox ID="txtEditRole" runat="server" Text='<%#Eval("Role") %>' />
     </EditItemTemplate>
     <ItemTemplate>
       <asp:Label ID="lblRole" runat="server" Text='<%#Eval("Role") %>' />
     </ItemTemplate>    
  </asp:TemplateField>
</Columns>
</asp:GridView>

.aspx.cs

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   //your code here..
}

To check complete article, checkout insert, update, delete gridview data example in asp.net.

immayankmodi
  • 8,210
  • 9
  • 38
  • 55
  • This doesn't work because I set the data source like follows. gridViewRoles.DataSource = Roles.GetAllRoles(); So it gives an error 'System.String' does not contain a property with the name 'Role'. – user966398 Oct 20 '14 at 21:07
  • That's because the "Role" field is not found from the resultset returned by "Roles.GetAllRoles();". Make sure "Role" field is returned within resultset and pass it as a List. – immayankmodi Oct 21 '14 at 04:07