【问题标题】:Button click event is not firing in asp.net gridview control按钮单击事件未在 asp.net gridview 控件中触发
【发布时间】:2014-05-15 15:09:52
【问题描述】:
 <asp:GridView ID="gvInaciveQuestions" runat="server" AutoGenerateColumns="False" OnRowCommand="gvInaciveQuestions_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Selelct">
                        <ItemTemplate>
                            <asp:Button ID="btnactive" runat="server"  Text="Active" onClick="btnactive_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>                   
                <asp:TemplateField HeaderText="Question">
 </asp:Gridview>

我已将 Itemtemplate 添加为 Button,但它没有触发 onclick 事件。

谁能推荐我?

【问题讨论】:

    标签: c# gridview controls


    【解决方案1】:

    不要在每次回发时使用Page.IsPostBack 属性来DataBind 它,例如在Page_Load 中(假设您用于数据绑定的方法称为BindGridView):

    protected void Page_Load(ovject sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGridView();
        }
    }
    

    否则不会触发事件,并且网格中更改的值会被您(旧)DataSource 中的值覆盖。

    【讨论】:

      【解决方案2】:

      您不应在此处使用onClick="btnactive_Click" 事件,而应使用CommandName 属性。

      <asp:Button ID="btnactive" runat="server"  Text="Active" CommandName="Click" />
      

      在你的OnRowCommand 中的代码隐藏中像这样实现。

       protected void gvInaciveQuestions_RowCommand(object sender, GridViewCommandEventArgs e)
                  {
                      if (e.CommandName == "Click")
                      {
                        //your code here
                      }
                  }
      

      希望这能解决您的问题。

      - 哈沙Ch

      【讨论】:

        猜你喜欢
        • 2010-10-21
        • 2011-12-02
        • 1970-01-01
        • 2015-10-11
        • 2014-01-14
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        相关资源
        最近更新 更多