RowUpdating not firing in EditItemTemplate

1k views Asked by At

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" />
1

There are 1 answers

2
immayankmodi On

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.