Rob
Rob

Reputation: 638

GridView OnRowUpdating does not fire due to CommandName="edit"

I already spend 2 days trying to solve this issue but no luck on how to solve this. I have a GridView to display data from the database then it also have functionality to modify and delete. This is the current ASP code for my GridView:

<asp:GridView ID="dgvSortKey" runat="server" AllowSorting="True" OnRowDataBound="gv_drb"
                AutoGenerateColumns="False" AllowPaging="True" BackColor="White" BorderColor="#336666"
                BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal"
                Height="73px" AutoGenerateEditButton="True" OnRowEditing="dgvSortKey_RowEditing"
                OnRowUpdating="dgvSortKey_RowUpdating" OnRowCancelingEdit="dgvSortKey_RowCancelingEdit"
                OnSelectedIndexChanging="dgvSortKey_SelectedIndexChanging" OnPageIndexChanged="dgvSortKey_PageIndexChanged"
                OnPageIndexChanging="dgvSortKey_PageIndexChanging" OnRowCommand="dgvSortKey_RowCommand"
                OnRowDeleted="dgvSortKey_RowDeleted" OnRowUpdated="dgvSortKey_RowUpdated" Width="561px"
                PageSize="15" DataKeyNames="KeyCode,KeyDescription">
                <FooterStyle BackColor="White" ForeColor="#333333" />
                <RowStyle BackColor="White" ForeColor="#333333" />
                <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                <AlternatingRowStyle BackColor="LightCyan" />
                <Columns>
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Instruction Key Code">
                        <ItemTemplate>
                            <asp:Label ID="lblValKeyCode" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyCode")) %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtValKeyCode" runat="server" Text='<%#Bind("KeyCode") %>' MaxLength="10"
                                CausesValidation="false"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="GvBorderGreen" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description">
                        <ItemTemplate>
                            <asp:Label ID="lblValKeyDescription" runat="server" Text='<%#System.Web.HttpUtility.HtmlEncode((string)Eval("KeyDescription")) %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtValKeyDescription" runat="server" Text='<%#Bind("KeyDescription") %>'
                                Width="300" MaxLength="20" CausesValidation="false"></asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle CssClass="GvBorderGreen" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

The problem is I can't update a certain record after click the Update Button, please see the image below:

enter image description here

When I'm in debug mode, it does not pass on OnRowUpdating event instead it passes to OnRowEditing. One thing that it makes me surprise is that when it fires to OnRowCommand, the CommandName set to "Edit" when Update Button is clicked. Please see the image below:

enter image description here

BTW this the Code Behind.

protected void dgvSortKey_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Currently Unreachable code due to unknown reason. RowUpdating not trigger even clicking Update Button in the GridView
        // this.dvDetialContent.Visible = true;
        List<SqlDbParameter> list = new List<SqlDbParameter>();

        TextBox txtValKeyDescription = dgvSortKey.Rows[e.RowIndex].FindControl("txtValKeyDescription") as TextBox;
        TextBox txtValKeyCode = dgvSortKey.Rows[e.RowIndex].FindControl("txtValKeyCode") as TextBox;

        if (string.IsNullOrEmpty(txtValKeyCode.Text) || string.IsNullOrEmpty(txtValKeyDescription.Text))
        {
            string alert = "alert('Instruction KeyCode or KeyDecription should not be empty');";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "scripting", alert, true);
        }
        else
        {
            //Trace.Write("txtKeyCode", HttpUtility.HtmlDecode(txtKeyCode.Text));
            //Trace.Write("txtKeyDescription", txtKeyDescription.Text);
            //Trace.Write("txtValKeyCode", txtValKeyCode.Text);
            //Trace.Write("txtValKeyDescription", txtValKeyDescription.Text);
            list.Add(new SqlDbParameter("@oldcode", HttpUtility.HtmlDecode(txtKeyCode.Text)));
            list.Add(new SqlDbParameter("@oldname", HttpUtility.HtmlDecode(txtKeyDescription.Text)));
            list.Add(new SqlDbParameter("@newcode", HttpUtility.HtmlDecode(txtValKeyCode.Text)));
            list.Add(new SqlDbParameter("@newname", HttpUtility.HtmlDecode(txtValKeyDescription.Text)));
            try
            {
                int result = CoreUtility.ExecuteNonQuery("update InstructionKey set KeyCode=@newcode, KeyDescription=@newname where KeyCode = @oldcode and KeyDescription = @oldname", list);
                //Trace.Write("ResultValue", result.ToString());
            }
            catch (Exception ex)
            {
                string alert = "alert('Instruction KeyCode should not be duplicate');";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "scripting2", alert, true);
            }
        }

        dgvSortKey.EditIndex = -1;
        imgbtnFilter_Click(null, null);

        this.txtKeyCode.Text = "";
        this.txtKeyDescription.Text = "";

    }
    protected void dgvSortKey_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // this.dvDetialContent.Visible = true;
        //if (ViewState["updateFlag"] == null)
        //{
            //ViewState["editFlag"] = "forEdit";
            //ViewState["editIndex"] = e.NewEditIndex;
            dgvSortKey.EditIndex = e.NewEditIndex;
            Label lblValKeyDescription = dgvSortKey.Rows[e.NewEditIndex].FindControl("lblValKeyDescription") as Label;
            Label lblValKeyCode = dgvSortKey.Rows[e.NewEditIndex].FindControl("lblValKeyCode") as Label;
            this.txtKeyCode.Text = lblValKeyCode.Text;
            this.txtKeyDescription.Text = lblValKeyDescription.Text;
            imgbtnFilter_Click(null, null);
        //}
        //else
        //{
            //RowUpdate((int)ViewState["editIndex"]);
            //ViewState.Remove("updateFlag");
            //ViewState.Remove("editFlag");
            //ViewState.Remove("editIndex");
        //}
    }
    protected void dgvSortKey_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgvSortKey.EditIndex = -1;
        imgbtnFilter_Click(null, null);
    }
    protected void dgvSortKey_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.dvDetialContent.Visible = true;
        Label lblValKeyDescription = dgvSortKey.Rows[e.NewSelectedIndex].FindControl("lblValKeyDescription") as Label;
        Label lblValKeyCode = dgvSortKey.Rows[e.NewSelectedIndex].FindControl("lblValKeyCode") as Label;

        this.txtKeyCode.Text = lblValKeyCode.Text;
        this.txtKeyDescription.Text = lblValKeyDescription.Text;
    }
    protected void dgvSortKey_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void dgvSortKey_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgvSortKey.PageIndex = e.NewPageIndex;
        imgbtnFilter_Click(null, null);
    }
    protected void dgvSortKey_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
    protected void dgvSortKey_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String jsScript = "";
        jsScript += "var answer=confirm('Delete this Instruction Key?');\n";
        jsScript += "if (!answer){\n";
        jsScript += " document.getElementById('ctl00_cthContent_hdDelete').Value = 'DELETE';\n";
        jsScript += "}\n";

        //return;
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", jsScript, true);

        List<SqlDbParameter> list = new List<SqlDbParameter>();

        Label lblValKeyCode = dgvSortKey.Rows[e.RowIndex].FindControl("lblValKeyCode") as Label;
        Label lblValKeyDescription = dgvSortKey.Rows[e.RowIndex].FindControl("lblValKeyDescription") as Label;
        list.Add(new SqlDbParameter("@code", lblValKeyCode.Text));
        list.Add(new SqlDbParameter("@name", lblValKeyDescription.Text));

        CoreUtility.ExecuteNonQuery("DELETE FROM [InstructionKey] WHERE KeyCode=@code and KeyDescription=@name;", list);
        Initial();
        this.dvDetialContent.Visible = false;
        dgvSortKey.EditIndex = -1;
        imgbtnFilter_Click(null, null);
    }
    protected void dgvSortKey_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandName;
    }
    protected void dgvSortKey_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void gv_drb(object sender, GridViewRowEventArgs e)// 
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
            //raising javascript confirmationbox whenver user clicks on link button
            lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox()");
        }
    }

Upvotes: 0

Views: 6078

Answers (1)

nunespascal
nunespascal

Reputation: 17724

While I have not figured out the bug, there is a workaround you may want to try.
Since the autogenerated edit button is giving you trouble, why not generate it yourself?

Like this:

<asp:TemplateField HeaderText="">
    <ItemTemplate>
        <asp:LinkButton ID="lnkedit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:LinkButton ID="lnkedit" runat="server" CommandName="Update">Update</asp:LinkButton>
        <asp:LinkButton ID="lnkedit" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
    </EditItemTemplate> 
</asp:TemplateField>

And handle it in your code behind:

  void dgvSortKey_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Update")
    {

    }
  }

Upvotes: 1

Related Questions