简体   繁体   English

通用事件处理程序未从动态按钮触发

[英]Generic event handler not firing from dynamic buttons

I have a bunch of buttons being dynamically created and setting its click event to an event handler...When I run it they handler in never firing, I've stepped through the code and nothing seems out of the ordinary but it just won't fire. 我有一堆按钮是动态创建的,并将其click事件设置为事件处理程序...当我运行它们时,它们的处理程序从未触发过,我已经遍历了代码,似乎没有什么异常,但它不会成功。开火。

Here is a snippet of the code where the button is being created.. 这是创建按钮的代码片段。

for (cellCtr = 1; cellCtr <= 9; cellCtr++)
            {

                Button button = new Button();

                HyperLink link = new HyperLink();

                TableCell tCell = new TableCell();

                if (rowCtr == rN)
                    break;

                string myStr = daccess.dsSubjects.Tables[0].Rows[rowCtr]["SubjectName"].ToString();
                int myID = Convert.ToInt32(daccess.dsSubjects.Tables[0].Rows[rowCtr]["SubjectID"].ToString());

                button.ID = Convert.ToString(myID);
                button.Text = myStr;

                button.CssClass = "btn btn-primary btn-sm"; 

                button.Click += ButtonSubjectClick;

                tCell.Controls.Add(button);

                tRow.Cells.Add(tCell);
                rowCtr++;

                if (cellCtr == rN)
                {
                    rowCtr = rowCtr - 1;
                    break;
                }
            }

and here is the handler that's it suppose to call... 这是应该调用的处理程序...

void ButtonSubjectClick(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        daccess.DeleteSubjects(Int32.Parse(button.ID.ToString()));
        button.CssClass = "btn-active";  
    }

When its ran, the source looks like this 运行后,源看起来像这样

<input type="submit" name="38" value="Math" id="38" class="btn btn-primary btn-sm" />

This code works for me: 该代码对我有用:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // do something if needed
    }

    // outside of postback, you need to add it each time
    Button button = new Button();
    button.ID = "btn1";
    button.Text = "clicky me";
    button.Click += new EventHandler(ButtonSubjectClick);
    pnl.Controls.Add(button);
}

void ButtonSubjectClick(object sender, EventArgs e)
{
    Button button = (Button)sender;
    button.Text = "clicked me";
}

I believe it's a question about how are you adding your button to the page. 我认为这是关于如何将按钮添加到页面的问题。 I'm adding my button at each postback and it is thus registered by the server. 我在每个回发处添加我的按钮,因此它已由服务器注册。

If you are adding your buttons in another event then you could use viewstate or session variables, store some kind of a flag to create the button and then check for this flag in each postback. 如果要在另一个事件中添加按钮,则可以使用viewstate或会话变量,存储某种标志来创建按钮,然后在每次回发中检查该标志。

Edit, for the sake of clarity, pnl is a Panel on my page: 为了清楚起见,编辑pnl是我页面上的一个面板:

<form id="form1" runat="server">
<div>
    <asp:Panel ID="pnl" runat="server"></asp:Panel>
</div>
</form>

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

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