c# - Dynamically Created Controls losing data after postback -


actually, creating 1 textbox on pageload , adding textbox panel. now, have linkbutton add another.

i entering text in textbox , if needed need create new textbox,by clicking add linkbutton.

actually, able count , recreate textboxes. but,the problem that, entered text in generated textboxes missing.

can anyone,suggest me solution this?

protected void page_load(object sender, eventargs e)     {         try         {             if (!ispostback)             {                 (int = 0; < 5; i++)                 {                     tablerow row = new tablerow();                     (int j = 0; j < 5; j++)                     {                         tablecell cell = new tablecell();                         textbox tb = new textbox();                                                 tb.id = "textboxrow_" + + "col_" + j;                                                 cell.controls.add(tb);                                                 row.cells.add(cell);                     }                                         table1.rows.add(row);                 }             }         }         catch (exception ex)         {             throw;         }             } 

this sample code, same code written in button_click also

 protected void aspxbutton1_click(object sender, eventargs e)     {  int k = table1.controls.count; } 

i getting count=0 on button_click.

when using dynamic controls, must remember exist until next postback.asp.net not re-create dynamically added control. if need re-create control multiple times, should perform control creation in pageload event handler ( creating first time textbox using condition: !ispostabck ). has additional benefit of allowing use view state dynamic control. though view state restored before page.load event, if create control in handler pageload event, asp.net apply view state information has after pageload event handler ends.

so, remove condition: !ispostback, each time page loads, textbox control created. see state of text box saved after pageload handler completes. [ have not disabled viewstate!!! ]

example:

protected void page_load(object sender, eventargs e) {      textbox txtbox = new textbox();     // assign text , id can retrieve later.       txtbox.id = "newbutton";     placeholder1.controls.add(txtbox);  } 

now after running it, type in text box , see happens when click button causes postback. text box still has maintained state!!!


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -