How to maintain dynamic control events, data during postback in asp.net

In asp.net if you add some dynamic control (button, linkbutton, textbox, checkbox etc) according to some condition and want to retain data, handle their event during postback then you should aware about asp.net server control life cycle.

Asp.net server control data, event maintain during postback -

Every server control which inherits IPostBackDataHandler interface has LoadPostData method that is for processing the postback data , when control implemented by a class (page, form, placeholders, controls etc), class calls the LoadPostData method and pass the posted data and key to maintain control states.
IPostBackDataHandler interface have LoadPostData method

Every server control has LoadViewState method to maintain their state during postback from posted viewstate when control implemented by a class (page, form, placeholders, controls etc), class call the LoadViewState method and pass the savedState (i.e viewstate for particular control) to process the viewstate data and maintain control state.

control have LoadViewState method to maintain their state during postback from posted viewstate
You should always be clear about LoadViewState, LoadPostData both are much different, LoadPostData is for loading posted user input data (like textbox text, chechbox check etc) and LoadViewState is for loading posted viewstate that have already been saved by the server to the page during postback.


All you need to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders then posted data will be assign automatically to the control by LoadPostData method and server state will be manage LoadViewState method.

Here controls re-instantiate / reinitialize each and every time within page load, so postback, viewstate data and events will never lost.

All Data And Event Maintain During PostBack -

protected void Page_Load(object sender, EventArgs e)
{
  if (Convert.ToString(ViewState["Generated"]) == "true")
      GenerateDynamicControls();

}
protected void btnGDynamicCont_Click(object sender, EventArgs e)
{
 if (Convert.ToString(ViewState["Generated"]) != "true")
 {
     GenerateDynamicControls();
     ViewState["Generated"] = "true";
 }
 else
 {
     Response.Write("<h2>Controls are already exist in page</h2>");
 }
}

public void GenerateDynamicControls()
{
 TextBox txtDynamic = new TextBox();
 txtDynamic.ID = "txtDynamic";
 txtDynamic.Text = "Dynamic TextBox";
 Page.Form.Controls.Add(txtDynamic);

 LinkButton lnkDynamic = new LinkButton();
 lnkDynamic.ID = "lnkDynamic";
 lnkDynamic.Click += new EventHandler(lnkDynamic_Click);
 lnkDynamic.Text = "Dynamic LinkButton";
 Page.Form.Controls.Add(lnkDynamic);
}

void lnkDynamic_Click(object sender, EventArgs e)
{
  Response.Write("<h2>Dynamic linkbutton clicked</h2>");
}

How to maintain dynamic control events, data during postback in asp.net demo
Only data through viewstate and user input data through posted data maintained by the asp.net not the control object so if this is dynamic control and it is not instantiating each and every request then contols will be lost so if asp.net not found the control in page after page load no state management will happen.

Here Controls Lost During PostBack No event will fire for linkbutton

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnGDynamicCont_Click(object sender, EventArgs e)
{
     GenerateDynamicControls();
 }

public void GenerateDynamicControls()
{
 TextBox txtDynamic = new TextBox();
 txtDynamic.ID = "txtDynamic";
 txtDynamic.Text = "Dynamic TextBox";
 Page.Form.Controls.Add(txtDynamic);

 LinkButton lnkDynamic = new LinkButton();
 lnkDynamic.ID = "lnkDynamic";
 lnkDynamic.Click += new EventHandler(lnkDynamic_Click);
 lnkDynamic.Text = "Dynamic LinkButton";
 Page.Form.Controls.Add(lnkDynamic);
}

void lnkDynamic_Click(object sender, EventArgs e)
{
  Response.Write("<h2>Dynamic linkbutton clicked</h2>");

}


Recent Article