Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Created Controls losing data after postback

Actually, I am Creating 1 TextBox on Pageload and adding that TextBox to Panel. Now, I have a LinkButton like Add Another.

I am entering Text in that TextBox and if needed I need to Create New TextBox,by clicking Add Another LinkButton.

Actually, I am able to get the count and recreate the TextBoxes. But,the Problem is that, My Entered text in the Previously Generated Textboxes is Missing.

Can Anyone,Suggest me a solution for this?

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                for (int i = 0; i < 5; i++)
                {
                    TableRow row = new TableRow();
                    for (int j = 0; j < 5; j++)
                    {
                        TableCell cell = new TableCell();
                        TextBox tb = new TextBox();                        
                        tb.ID = "TextBoxRow_" + i + "Col_" + j;                        
                        cell.Controls.Add(tb);                        
                        row.Cells.Add(cell);
                    }                    
                    Table1.Rows.Add(row);
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }        
    }

This is a Sample Code, the same code is written in Button_Click Also

 protected void ASPxButton1_Click(object sender, EventArgs e)
    {
 int k = Table1.Controls.Count;
}

I am getting a Count=0 on Button_Click.

like image 827
RealSteel Avatar asked Jul 11 '13 09:07

RealSteel


People also ask

How can we solve the problem of dynamic control disappearing after post back?

In order to retain the dynamic controls across PostBacks, you need to make use of Page's PreInit event to recreate the dynamic controls.

How to maintain dynamic control events data during PostBack in asp net?

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.


4 Answers

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

enter image description here

like image 52
Vivek Avatar answered Oct 19 '22 19:10

Vivek


When using dynamic controls, you must remember that they will exist only until the next postback.ASP.NET will not re-create a dynamically added control. If you need to re-create a control multiple times, you should perform the control creation in the PageLoad event handler ( As currently you are just creating only for first time the TextBox using Condition: !IsPostabck ). This has the additional benefit of allowing you to use view state with your dynamic control. Even though view state is normally restored before the Page.Load event, if you create a control in the handler for the PageLoad event, ASP.NET will apply any view state information that it has after the PageLoad event handler ends.

So, Remove the Condition: !IsPostback, So that each time the page Loads, The TextBox control is also created. You will also see the State of Text box saved after PageLoad handler completes. [ Obviously you have not disabled ViewState!!! ]

Example:

protected void Page_Load(object sender, EventArgs e)
{

    TextBox txtBox = new TextBox();
    // Assign some text and an ID so you can retrieve it later. 

    txtBox.ID = "newButton";
    PlaceHolder1.Controls.Add(txtBox);

}

Now after running it, type anything in text box and see what happens when you click any button that causes postback. The Text Box still has maintained its State!!!

like image 28
R.C Avatar answered Oct 19 '22 19:10

R.C


The dynamically generated control do not maintain state. You have to maintain it at your own. You can use some hidden field to keep the state of controls, which will be used on server side to extract the state. Asp.net uses hidden field to maintain the state between requests, you can see __VIEWSTATE in the source.

In ASP.NET pages, the view state represents the state of the page when it was last processed on the server. It's used to build a call context and retain values across two successive requests for the same page. By default, the state is persisted on the client using a hidden field added to the page and is restored on the server before the page request is processed. The view state travels back and forth with the page itself, but does not represent or contain any information that's relevant to client-side page display, Reference.

like image 7
Adil Avatar answered Oct 19 '22 20:10

Adil


Just remove this line

 if (!IsPostBack)
like image 4
Seçkin Durgay Avatar answered Oct 19 '22 20:10

Seçkin Durgay