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

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.

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)

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Refreshing page in Javascript

In this article we will see that how to refresh or reload a page through Java Script there are a lot of ways to refresh or reload a document depending how we want (from server side