由于CommandName =“edit”,GridView OnRowUpdating不会触发

我已经花了两天时间试图解决这个问题,但没有运气如何解决这个问题。 我有一个GridView来显示数据库中的数据,然后它还具有修改和删除function。 这是我的GridView的当前ASP代码:

          Delete     <asp:Label ID="lblValKeyCode" runat="server" Text=''>   <asp:TextBox ID="txtValKeyCode" runat="server" Text='' MaxLength="10" CausesValidation="false">      <asp:Label ID="lblValKeyDescription" runat="server" Text=''>   <asp:TextBox ID="txtValKeyDescription" runat="server" Text='' Width="300" MaxLength="20" CausesValidation="false">      

问题是我点击更新按钮后无法更新某个记录,请看下图:

在此处输入图像描述

当我处于调试模式时,它不传递OnRowUpdating事件而是传递给OnRowEditing。 令我惊讶的一件事是,当它触发OnRowCommand时,CommandName在单击Update Button时设置为“Edit” 。 请看下图:

在此处输入图像描述

顺便说一句,这是守则背后的原因。

 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 list = new List(); 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 list = new List(); 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()"); } } 

虽然我还没有弄清楚这个bug,但您可能需要尝试一种解决方法。
由于自动生成的编辑按钮给您带来麻烦,为什么不自己生成呢?

像这样:

   Edit   Update Cancel   

并在你的代码中处理它:

  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") { } }