Dynamically Created Controls Losing Data After Postback

Dynamically Created Controls losing data after postback

All you need to do is 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, the posted data will automatically be assigned to the control by calling the LoadPostData method by the parent control.

check the article and how to write code for dynamic control -
How to maintain dynamic control events, data during postback in asp.net

Sample Image

Dynamically created Textboxes loose value on PostBack

Maybe this MSDN article explains why this happens: Dynamic Web Server Controls and View State

You will find a good explanation in this older article: Dynamic Web Controls, Postbacks, and View State

If the dynamic controls are created on each page request, they will pick up the ViewState values from the previous request, so maybe create the controls on load, hide them and show them when the user does something.

Also, if you have controls created at design time that have this behavior of losing the values after postback, check that you don't set their EnableViewState property to false somewhere.

And you should set the Name of the control along it's Id. The name is used in the form submission. This is best seen when you have a list of radio buttons with different ids and the same name. When submitted, a pair with the name of the radio buttons as key and selected radio button's id as value will be sent.

Why do my dynamically added controls loose their values after Postback?

Each control that should receive data from page ViewState should be instantiated in Init or Load event handlers, because ViewState is persisted to controls BEFORE Click, Change and the rest control events (those events are triggered when ViewState changes are detected, so ViewState must be read before Click event is fired).

So the process should look like:

  1. OnInit (static controls get created)
  2. Static control content is deserialized from ViewState
  3. OnLoad (create dynamic controls, in your case textboxes that you created in last Postback)
  4. Dynamic control content is deserialized from ViewState
  5. Click, Change and other events are fired according to changes detected comparing POST data and ViewState data

Suggestions:

You can use hidden fields to save additional status information, and then in OnLoad you can read that info to recreate dynamically created controls.

Also, you should explicitly set ID property of your textboxes so that values can be properly persisted back, don't rely on ASP.Net.

Dynamic repeater controls getting lost after post back

If you need to create dynamic controls you have to recreate them on every postback. So ItemDataBound is inappropriate since it's only triggered when the repeater is getting databound. Use ItemCreated instead.

protected void myRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
if (** condition 01 **)
{
if (** condition 02 **)
{
RadioButton rdoBtn = new RadioButton();
rdoBtn.ID = "rbtnID";
rdoBtn.EnableViewState = true;
rdoBtn.GroupName = "GroupName";
rdoBtn.AutoPostBack = true;
rdoBtn.CheckedChanged += new System.EventHandler(this.rdoBtnChecked_Changed);
string script = "SetUniqueRadioButton('myRepeater.*GroupName',this)";
rdoBtn.Attributes.Add("onclick", script);
Panel pnlRbtnSet = e.Item.FindControl("pnlSelect") as Panel;
pnlRbtnSet.Controls.Add(rdoBtn);
}
else
{
CheckBox chkBox = new CheckBox();
chkBox.ID = "chkBxID";
chkBox.Checked = true;
chkBox.EnableViewState = true;
Panel pnlChkBoxesSet = e.Item.FindControl("pnlSelect") as Panel;
pnlChkBoxesSet.Controls.Add(chkBox);
}
}
}
}

Dynamically added controls in gridview (checkbox) disappear after postback in asp.net

When using dynamic controls you need to rebind the GridView data on every PostBack. So usually you would use an IsPostBack check and bind data in there. But do not do that now.

protected void Page_Load(object sender, EventArgs e)
{
//normally you would bind here
if (IsPostBack == false)
{
GridView1.DataSource = source;
GridView1.DataBind();
}

//but when using dynamic control inside a gridview, bind here
GridView1.DataSource = source;
GridView1.DataBind();
}

Update

And you have to give a dynamic control an ID. You are looking for cb1, but you never assign that ID to the checkbox.

CheckBox cb1 = new CheckBox();
cb1.ID = "cb1";

Avoiding postbacks on dynamically created controls

The key to dynamically created controls is when in the page lifecyle to add the controls. To retain viewstate and have the dynamic controls maintain the posted values, the controls need to be added no later than OnInit().

In addition, the controls need to be re-added on every page load, postback or not. Regarding what @KennyZ said about guaranteeing the same ID for each control, as long as you are adding the same number of controls in the same order, .NET will guarantee the IDs are the same.

In regards to the AutoPostBack property, this is usually only set to True when we want to perform some action that is triggered by the user changing the value of the control. A common example is a drop-down list; depending on what the user selected, different data is loaded into, say, the details section of the page. So each time the user changes the value, we want to postback and fetch new data to display.

From my understanding of your use case, I would suggest one postback for the entire form.



Related Topics



Leave a reply



Submit