OnRowCommand is triggered, OnRowEditing not firing on my gridview

141 views Asked by At

I have a gridview like below:

<asp:GridView ID="gvitems" runat="server" AutoGenerateColumns="false" CssClass="GridStyle" AllowSorting="true" OnSorting="OnSorting" DataKeyNames="Id" OnRowCommand="gv_RowCommand" 
             OnRowEditing="gv_RowEditing" OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelEdit" >    
             <Columns>    
                 <asp:buttonfield buttontype="Link" 
                              commandname="View"
                              text="View"/>

                 <asp:TemplateField>
                     <ItemTemplate>
                         <asp:Button runat="server" Text="Edit" />
                     </ItemTemplate>
                     <EditItemTemplate>
                         <asp:Button runat="server" Text="Update" />
                     </EditItemTemplate>
                 </asp:TemplateField>

          
                 <asp:TemplateField HeaderText="Next Steps">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblNextSteps" Text='<%# Eval("NextSteps")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtNextSteps" Text='<%# Eval("NextSteps")%>'></asp:TextBox>
                    </EditItemTemplate>
                     <FooterTemplate>
                         <asp:TextBox runat="server" ID="txtNextStepsFooter"></asp:TextBox>
                     </FooterTemplate>
                </asp:TemplateField>

</Columns>    
</asp:GridView>

And in the backend, this is how i fill it with data:

DataSet dataSet = null;
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      this.BindGrid_SubmittedProjects();
   }
}

private void BindGrid_SubmittedProjects(string sortExpression = null)
    {
        using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ProjectsDataBase"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("ViewProjects", con)) 
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = "myusername";
                cmd.Connection = con;

                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    dataSet = new DataSet();
                    sda.Fill(dataSet, "Project");
                }

                gvitems.DataSource = dataSet.Tables[0].DefaultView;
                gvitems.DataBind();
            }
        }
    }

When I click on the "View" hyperlink on each row, it works, so OnRowCommand function below is triggered.

protected void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "View")
        {
            //my work
        }
    }

However, when I click on "Edit" button, nothing is happening, OnRowEditing is not being triggered.

protected void gv_RowEditing(Object sender, GridViewEditEventArgs e)
        {
            gvitems.EditIndex = e.NewEditIndex;
            this.BindGrid_SubmittedProjects();
        }

        protected void gv_RowUpdating(Object sender, GridViewUpdateEventArgs e)
        {
            //my update query.
        }

I could not see what I am doing wrong. Tried many things I've found over online but none worked. Any help would be so appreciated.

Regards.

1

There are 1 answers

0
Eray Balkanli On BEST ANSWER

Using LinkButton instead of regular button fixed the issue.

<asp:TemplateField>
                     <ItemTemplate>
                         <asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
                         <asp:LinkButton Text="Delete" runat="server" CommandName="Delete" />
                     </ItemTemplate>
                     <EditItemTemplate>
                         <asp:LinkButton Text="Update" runat="server" CommandName="Update" />
                         <asp:LinkButton Text="Cancel" runat="server" CommandName="Cancel" />
                     </EditItemTemplate>
</asp:TemplateField>