简体   繁体   中英

Adding click event dynamically is not working

first time questioner here.

I have CSharp code in VS2017 as below - it is placed in only the first row of a dynamic table:

 if(intN == 1)
 {
    tc = new TableCell();
    tc.CssClass = "FNT_14_Bold";
    btn = new Button();
    btn.ID = "btnSubmitCurrentUserChanges";
    btn.Height = 25;
    btn.Width = 261;
    btn.Text = "Submit Current Users Changes";
    btn.BackColor = ColorTranslator.FromHtml("#006F6F");
    btn.ForeColor = Color.White;
    btn.Click += new EventHandler(btnUsers_Click);
    tc.Controls.Add(btn);
}
else
{
    tc = new TableCell();
    tc.VerticalAlign = VerticalAlign.Top;
    tc.HorizontalAlign = HorizontalAlign.Center;
    tc.CssClass = "FNT_14";
}

protected void btnUsers_Click(object sender, EventArgs e)
{
    SubmitData();
}

When I click on the button, it fires, but only as far as the Page_Load. Any suggestions on getting it to the actual event?

Thanks in advance... StackOverflow has been a great help.

I assume the first part of your code is from Page_Load . You need to move it to Page_Init . That way the dynamically created controls are ready to fire the events when the page has loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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