事件处理程序未在动态按钮单击时启动

时间:2022-03-26 05:55:48

I have a dynamically created button with an onclick event handler. The problem is that when I click the button it does not hit the event in the code-behind.

我有一个动态创建的按钮和一个onclick事件处理程序。问题是,当我单击按钮时,它不会在代码隐藏中击中事件。

protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataTable dt = ds.Tables[0];
    DropDownList ddl = new DropDownList();
    TextBox txt = new TextBox();
    int index = 1;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ddl = e.Row.FindControl("ddlNewO") as DropDownList;
        txt = e.Row.FindControl("txtNewT") as TextBox;
    }
    foreach (DataRow r in dt.Rows)
    {
        string listitem = Convert.ToString(index);
        ddl.Items.Add(listitem);
        index++;
    }
    ddl.SelectedIndex = e.Row.RowIndex;
    if (e.Row.RowIndex == 0)
    {
        ddl.Enabled = false;
        txt.Enabled = false;
    }
    else if (e.Row.RowIndex != 0)
    {
        ddl.Items.Remove("1");
        //Create ED button


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnED = new Button();
            btnED.ID = "btnED";
            btnED.CssClass = "buttonsmall";
            //btnED.CommandName = "ED";
            btnED.EnableViewState = true;
            btnED.Click += new EventHandler(btnED_Click);
            foreach (DataRow r in dt.Rows)
            {
                btnED.Attributes.Add("ID", r.ItemArray[2].ToString());
            if (r.ItemArray[3].ToString() == "1")
            {
                btnED.Text = "Disable";
            }
            else 
            {
                btnED.Text = "Enable";
            }
            //Add button to grid
            e.Row.Cells[5].Controls.Add(btnED); 
            }   
        }


    }

}
protected void btnED_Click(object sender, EventArgs e)
{
    // Coding to click event
}

2 个解决方案

#1


2  

So the problem here is that when the page is being recreated on post back - there is no more button! Dynamic controls need to be added on the page on every post back to fire events properly. In your case however on the first load when the GridView is binding you add the button to the page. But on the post back after the click the button is not added again, because GridView is not data bound again. Therefore ASP.NET cannot derive the source of the event, and supresses it.

所以这里的问题是,当页面被重新创建到post back时——没有更多的按钮!需要在每个帖子的页面上添加动态控件以正确地触发事件。在您的情况下,但是在GridView绑定时的第一次加载中,您将按钮添加到页面。但在单击后的post上,按钮不会再次添加,因为GridView不再绑定数据。因此ASP。NET不能派生事件的源,并压制它。

Fix here is to bind GridView with data on every post back. Literally if you had if (!IsPostBack) - remove it. Or you can add the button in the template field and play with visibility - may be an approach as well.

这里的解决方案是将GridView与每个post的数据绑定在一起。如果你有if (!IsPostBack) -删除它。或者您可以在模板字段中添加按钮并使用可见性——这也可能是一种方法。

#2


0  

You need to add a click handler on Row Created not on Data Bound I believe.

您需要在创建的行上添加一个单击处理程序,而不是在数据绑定上。

protected void gvOrderRowCreated(object sender, GridViewRowEventArgs e)
{
    switch (e.Row.RowType) {
        case DataControlRowType.DataRow:
            Button btn = (Button)e.Row.FindControl("btnED");
            btn.Command += btnED_Click;
            break;
    }
}

#1


2  

So the problem here is that when the page is being recreated on post back - there is no more button! Dynamic controls need to be added on the page on every post back to fire events properly. In your case however on the first load when the GridView is binding you add the button to the page. But on the post back after the click the button is not added again, because GridView is not data bound again. Therefore ASP.NET cannot derive the source of the event, and supresses it.

所以这里的问题是,当页面被重新创建到post back时——没有更多的按钮!需要在每个帖子的页面上添加动态控件以正确地触发事件。在您的情况下,但是在GridView绑定时的第一次加载中,您将按钮添加到页面。但在单击后的post上,按钮不会再次添加,因为GridView不再绑定数据。因此ASP。NET不能派生事件的源,并压制它。

Fix here is to bind GridView with data on every post back. Literally if you had if (!IsPostBack) - remove it. Or you can add the button in the template field and play with visibility - may be an approach as well.

这里的解决方案是将GridView与每个post的数据绑定在一起。如果你有if (!IsPostBack) -删除它。或者您可以在模板字段中添加按钮并使用可见性——这也可能是一种方法。

#2


0  

You need to add a click handler on Row Created not on Data Bound I believe.

您需要在创建的行上添加一个单击处理程序,而不是在数据绑定上。

protected void gvOrderRowCreated(object sender, GridViewRowEventArgs e)
{
    switch (e.Row.RowType) {
        case DataControlRowType.DataRow:
            Button btn = (Button)e.Row.FindControl("btnED");
            btn.Command += btnED_Click;
            break;
    }
}