Skip to main content

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>");

}


Popular posts from this blog

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Scrollable Gridview with fixed headers in asp.net

horizontal and vertical scrollable Gridview with static header row.fixed header in gridview in asp.net.static header in gridview through java script.I was looking for a solution for this for a long time and found most of the answers are not working or not suitable for my situation i also find most of the java script code for that they worked but only with the vertical scroll not with the horizontal scroll and also combination of header and rows doesn't match.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview, Scrollble gridview with fixed header through javascript, File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview.

We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)