Controls that are created at runtime have to be called either
OnPageLoad
or
OnPageInit
without checking
IsPostBack
property of the page. This will gave the controls to retain their associate events and properties on the page. For example,:Assuming that you have a div with an ID
div1
that runs as a sever(
runat="server"
) control, The following code will add a
Button "Submit"
with it's event to a page.
protected void Page_Load(object sender, EventArgs e)
{
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
btnSubmit.Click += new EventHandler(btnSubmit_Click);
div1.Controls.Add(btnSubmit);
}
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
}