GridView控件OnRowUpdating不火因的CommandName ="编辑" [英] GridView OnRowUpdating does not fire due to CommandName="edit"

查看:134
本文介绍了GridView控件OnRowUpdating不火因的CommandName ="编辑"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花2天尝试解决这个问题,但对如何解决这个没有运气。我有一个GridView显示从数据库中的数据,然后它也具有的功能进行修改和删除。这是当前ASP code为我的GridView的:

 < ASP:GridView控件ID =dgvSortKey=服务器AllowSorting =真OnRowDataBound =gv_drb
                的AutoGenerateColumns =FALSEAllowPaging =真背景色=白BORDERCOLOR =#336666
                边框样式=双边框宽度=的3px的cellpadding =4网格=水平
                HEIGHT =73pxAutoGenerateEditButton属性=真OnRowEditing =dgvSortKey_RowEditing
                OnRowUpdating =dgvSortKey_RowUpdatingOnRowCancelingEdit =dgvSortKey_RowCancelingEdit
                OnSelectedIndexChanging =dgvSortKey_SelectedIndexChangingOnPageIndexChanged =dgvSortKey_PageIndexChanged
                OnPageIndexChanging =dgvSortKey_PageIndexChangingOnRowCommand =dgvSortKey_RowCommand
                OnRowDeleted =dgvSortKey_RowDeletedOnRowUpdated =dgvSortKey_RowUpdated宽度=561px
                每页=15的DataKeyNames =键code,KeyDescription>
                < FooterStyle背景色=白前景色=#333333/>
                < RowStyle背景色=白前景色=#333333/>
                < PagerStyle背景色=#336666前景色=白Horizo​​ntalAlign =中心/>
                < SelectedRowStyle背景色=#339966FONT-粗体=真前景色=白/>
                < HeaderStyle背景色=#336666FONT-粗体=真前景色=白Horizo​​ntalAlign =中心/>
                < AlternatingRowStyle背景色=浅青绿/>
                <柱体和GT;
                    < ASP:的TemplateField的HeaderText =>
                        <&ItemTemplate中GT;
                            < ASP:LinkBut​​ton的ID =lnkdelete=服务器的OnClick =lnkdelete_Click>删除< / ASP:LinkBut​​ton的>
                        < / ItemTemplate中>
                    < / ASP:的TemplateField>
                    < ASP:的TemplateField的HeaderText =指令键code>
                        <&ItemTemplate中GT;
                            < ASP:标签ID =lblValKey code=服务器文本='<%#System.Web.HttpUtility.HtmlEn code((串)的eval(键code ))%GT;'>< / ASP:标签>
                        < / ItemTemplate中>
                        <&EditItemTemplate的GT;
                            < ASP:文本框ID =txtValKey code=服务器文本='<%#绑定(键code)%GT;' MAXLENGTH =10
                                的CausesValidation =false的>< / ASP:文本框>
                        < / EditItemTemplate中>
                        < ItemStyle Horizo​​ntalAlign =中心VerticalAlign =中东的CssClass =GvBorderGreen/>
                    < / ASP:的TemplateField>
                    < ASP:的TemplateField的HeaderText =说明>
                        <&ItemTemplate中GT;
                            < ASP:标签ID =lblValKeyDescription=服务器文本='<%#System.Web.HttpUtility.HtmlEn code((串)的eval(KeyDescription))%GT;'> < / ASP:标签>
                        < / ItemTemplate中>
                        <&EditItemTemplate的GT;
                            < ASP:文本框ID =txtValKeyDescription=服务器文本='<%#绑定(KeyDescription)%>'
                                宽度=300MAXLENGTH =20的CausesValidation =假>&下; / ASP:文本框>
                        < / EditItemTemplate中>
                        < ItemStyle的CssClass =GvBorderGreen/>
                    < / ASP:的TemplateField>
                < /专栏>
            < / ASP:GridView的>

问题是我不能更新某条记录点击更新按钮后,请参见下图:

当我在调试模式是,它不OnRowUpdating事件票代替其传递到OnRowEditing。有一件事情,这让我惊喜的是,当它发射到OnRowCommand的CommandName的设置为 编辑 更新按钮的< >点击/强。请看下图:

BTW这个code的后面。

 保护无效dgvSortKey_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
    {
        //当前无法访问code,由于未知的原因。 RowUpdating不会触发即使单击GridView的更新按钮
        // this.dvDetialContent.Visible = TRUE;
        清单&LT; SqlDbParameter&GT;名单=新名单,LT; SqlDbParameter&GT;();        文本框txtValKeyDescription = dgvSortKey.Rows [e.RowIndex] .FindControl(txtValKeyDescription)的文本框;
        文本框txtValKey code = dgvSortKey.Rows [e.RowIndex] .FindControl(txtValKey code)的文本框;        如果(string.IsNullOrEmpty(txtValKey code.Text)|| string.IsNullOrEmpty(txtValKeyDescription.Text))
        {
            字符串警报=警报('指令键code或KeyDecription不能为空);;
            ScriptManager.RegisterStartupScript(这一点,this.GetType(),脚本,警惕,真实);
        }
        其他
        {
            //Trace.Write(\"txtKey$c$c,HttpUtility.HtmlDe code(txtKey code.Text));
            //Trace.Write(\"txtKeyDescription,txtKeyDescription.Text);
            //Trace.Write(\"txtValKey$c$c,txtValKey code.Text);
            //Trace.Write(\"txtValKeyDescription,txtValKeyDescription.Text);
            list.Add(新SqlDbParameter(@老code,HttpUtility.HtmlDe code(txtKey code.Text)));
            list.Add(新SqlDbParameter(@使用oldName,HttpUtility.HtmlDe code(txtKeyDescription.Text)));
            list.Add(新SqlDbParameter(@新的code,HttpUtility.HtmlDe code(txtValKey code.Text)));
            list.Add(新SqlDbParameter(@ NEWNAME,HttpUtility.HtmlDe code(txtValKeyDescription.Text)));
            尝试
            {
                INT结果= CoreUtility.ExecuteNonQuery(更新InstructionKey SET键code = @新的code,KeyDescription = @ NEWNAME其中key code = @老code和KeyDescription = @oldname名单) ;
                //Trace.Write(\"ResultValue,result.ToString());
            }
            赶上(异常前)
            {
                字符串警报=警报('指令键code不应该重复');;
                ScriptManager.RegisterStartupScript(这一点,this.GetType(),scripting2,警惕,真实);
            }
        }        dgvSortKey.EditIndex = -1;
        imgbtnFilter_Click(NULL,NULL);        this.txtKey code.Text =;
        this.txtKeyDescription.Text =;    }
    保护无效dgvSortKey_RowEditing(对象发件人,GridViewEditEventArgs E)
    {
        // this.dvDetialContent.Visible = TRUE;
        //如果(的ViewState [updateFlag] == NULL)
        // {
            //的ViewState [editFlag] =forEdit;
            //的ViewState [editIndex] = e.NewEditIndex;
            dgvSortKey.EditIndex = e.NewEditIndex;
            标签lblValKeyDescription = dgvSortKey.Rows [e.NewEditIndex] .FindControl(lblValKeyDescription)作为标签;
            标签lblValKey code = dgvSortKey.Rows [e.NewEditIndex] .FindControl(lblValKey code)作为标签;
            this.txtKey code.Text = lblValKey code.Text;
            this.txtKeyDescription.Text = lblValKeyDescription.Text;
            imgbtnFilter_Click(NULL,NULL);
        //}
        //其他
        // {
            // RowUpdate((INT)的ViewState [editIndex]);
            //ViewState.Remove(\"updateFlag);
            //ViewState.Remove(\"editFlag);
            //ViewState.Remove(\"editIndex);
        //}
    }
    保护无效dgvSortKey_RowCancelingEdit(对象发件人,GridViewCancelEditEventArgs E)
    {
        dgvSortKey.EditIndex = -1;
        imgbtnFilter_Click(NULL,NULL);
    }
    保护无效dgvSortKey_SelectedIndexChanging(对象发件人,GridViewSelectEventArgs E)
    {
        this.dvDetialContent.Visible = TRUE;
        标签lblValKeyDescription = dgvSortKey.Rows [e.NewSelectedIndex] .FindControl(lblValKeyDescription)作为标签;
        标签lblValKey code = dgvSortKey.Rows [e.NewSelectedIndex] .FindControl(lblValKey code)作为标签;        this.txtKey code.Text = lblValKey code.Text;
        this.txtKeyDescription.Text = lblValKeyDescription.Text;
    }
    保护无效dgvSortKey_PageIndexChanged(对象发件人,EventArgs的发送)
    {    }
    保护无效dgvSortKey_PageIndexChanging(对象发件人,GridViewPageEventArgs E)
    {
        dgvSortKey.PageIndex = e.NewPageIndex;
        imgbtnFilter_Click(NULL,NULL);
    }
    保护无效dgvSortKey_RowUpdated(对象发件人,GridViewUpdatedEventArgs E)
    {    }
    保护无效dgvSortKey_RowDeleting(对象发件人,GridViewDeleteEventArgs E)
    {
        串jsScript =;
        jsScript + =VAR答案=确认('删除此指令键?'); \\ N的;
        jsScript + =如果(!回答){\\ n;
        jsScript + =的document.getElementById('ctl00_cthContent_hdDelete')值='删除'。\\ n;
        jsScript + =} \\ n;        //返回;
        ScriptManager.RegisterStartupScript(这一点,this.GetType(),剧本,jsScript,真正的);        清单&LT; SqlDbParameter&GT;名单=新名单,LT; SqlDbParameter&GT;();        标签lblValKey code = dgvSortKey.Rows [e.RowIndex] .FindControl(lblValKey code)作为标签;
        标签lblValKeyDescription = dgvSortKey.Rows [e.RowIndex] .FindControl(lblValKeyDescription)作为标签;
        list.Add(新SqlDbParameter(@ code,lblValKey code.Text));
        list.Add(新SqlDbParameter(@名,lblValKeyDescription.Text));        CoreUtility.ExecuteNonQuery(删除[InstructionKey]其中key code = @ code和KeyDescription = @名;清单);
        初始();
        this.dvDetialContent.Visible = FALSE;
        dgvSortKey.EditIndex = -1;
        imgbtnFilter_Click(NULL,NULL);
    }
    保护无效dgvSortKey_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        字符串ID = e.CommandName;
    }
    保护无效dgvSortKey_RowDeleted(对象发件人,GridViewDeletedEventArgs E)
    {    }
    保护无效gv_drb(对象发件人,GridViewRowEventArgs E)//
    {
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkBut​​ton的lnkbtnresult =(LinkBut​​ton的)e.Row.FindControl(lnkdelete);
            //提高JavaScript的confirmationbox whenver用户点击链接按钮
            lnkbtnresult.Attributes.Add(onclick事件的javascript:返回ConfirmationBox());
        }
    }


解决方案

虽然我还没有想出的bug,还有就是你可能想尝试一种解决方法。结果
由于自动生成的编辑按钮是给你的麻烦,为什么不自己产生的呢?

这样的:

 &LT; ASP:的TemplateField的HeaderText =&GT;
    &LT;&ItemTemplate中GT;
        &LT; ASP:LinkBut​​ton的ID =lnkedit=服务器的CommandName =编辑&gt;编辑&LT; / ASP:LinkBut​​ton的&GT;
    &LT; / ItemTemplate中&GT;
    &LT;&EditItemTemplate的GT;
        &LT; ASP:LinkBut​​ton的ID =lnkedit=服务器的CommandName =更新&GT;更新和LT; / ASP:LinkBut​​ton的&GT;
        &LT; ASP:LinkBut​​ton的ID =lnkedit=服务器的CommandName =取消&GT;取消&LT; / ASP:LinkBut​​ton的&GT;
    &LT; / EditItemTemplate中&GT;
&LT; / ASP:的TemplateField&GT;

和处理它在你的背后code:

 无效dgvSortKey_RowCommand(对象发件人,GridViewCommandEventArgs E)
  {
    //如果有多个按钮在GridView控件中使用,使用
    // CommandName属性,以确定哪个按钮被点击。
    如果(e.CommandName ==更新)
    {    }
  }

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:

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:

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()");
        }
    }

解决方案

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

    }
  }

这篇关于GridView控件OnRowUpdating不火因的CommandName =&QUOT;编辑&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆