简体   繁体   English

在ASP.NET GridView控件中未触发按钮单击事件

[英]Button click event is not firing in asp.net gridview control

 <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>

I have added Itemtemplate as Button but it is not firing onclick event. 我已将Itemtemplate添加为Button,但未触发onclick事件。

Could please any one suggest me ? 有人可以建议我吗?

Don't DataBind it on every postback by using the Page.IsPostBack property, for example in Page_Load (resuming that the method that you're using for databinding is called BindGridView ): 不要DataBind使用它在每一个回传Page.IsPostBack属性,例如在Page_Load (即恢复您使用数据绑定的方法被称为BindGridView ):

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

Otherwise events aren't triggered and changed values in the grid are overwritten by the values from your (old) DataSource . 否则,不会触发事件,并且网格中更改的值将被您(旧) DataSource的值覆盖。

you should not use onClick="btnactive_Click" event here, instead use CommandName property. 您不应在此处使用onClick="btnactive_Click"事件,而应使用CommandName属性。

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

In your OnRowCommand in codebehind implement like this. 在您的OnRowCommand中的代码OnRowCommand实现这样。

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

I hope this will solve your issue. 希望这能解决您的问题。

- Harsha Ch -Harsha Ch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM