I have few dynamically generated tables with textbox or dropdownlist controls inside. Here are my questions:
1. where is the best place/event handler to save these controls' value into viewstate druing postback? the rows of the table(s) can be accesed or should i access the web controls via their ID (ugly) directly?
2. where is the place to re-create these tables based on the objects in viewstate?
As this is a State Management forum, I was looking for some details on the topic. I did some search found that there is one mentioning about saving table's control(textbox...)s' value in SaveViewState() but re-populate the table in LoadViewState(), confusing
me. Any insight?
in this program, if you dynamic create the control in page_load, then you click the button, the message in textbox still remain. When you put the code in PreRender, then the message will disappear.
But if you want to store the message to viewstate by this: viewstate["control1"]=textbox1.text, I think you need to specific the textbox id and reload it after dynamic created.
As this is a State Management forum, I was looking for some details on the topic. I did some search found that there is one mentioning about saving table's control(textbox...)s' value in SaveViewState() but re-populate the table in LoadViewState(), confusing
me. Any insight?
I understand its a state management forum and the links I provided talk about state management if you read the documents. Dynamic controls should be created in Page.Init as ViewState is not available until Page_Load.
So if you read the initial documents I posted and the 2 documents above you will see the importance of ViewState and when it is loaded and how to save data to it. If your controls are created on Page.Init and you save the values to ViewState on Page.Load
then on Page.PreRender you can gather those values from ViewState and assign them to your controls. SaveViewState and LoadViewState have arrays of objects. So you access them array index when you save or look for objects to get values from.
Remember to mark as answer if this post answered or solved your problem.
seanad
Member
36 Points
41 Posts
Dynamic table content
Jan 25, 2012 11:39 AM|LINK
Hi all,
I have few dynamically generated tables with textbox or dropdownlist controls inside. Here are my questions:
1. where is the best place/event handler to save these controls' value into viewstate druing postback? the rows of the table(s) can be accesed or should i access the web controls via their ID (ugly) directly?
2. where is the place to re-create these tables based on the objects in viewstate?
TIA
-s
b471code3
Star
13877 Points
2598 Posts
Re: Dynamic table content
Jan 25, 2012 12:20 PM|LINK
Dynamic controls should be created/recreated in Page_Init event. Check out the MSDN documentation on Page Life Cycle
seanad
Member
36 Points
41 Posts
Re: Dynamic table content
Jan 25, 2012 08:27 PM|LINK
Thanks.
As this is a State Management forum, I was looking for some details on the topic. I did some search found that there is one mentioning about saving table's control(textbox...)s' value in SaveViewState() but re-populate the table in LoadViewState(), confusing me. Any insight?
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: Dynamic table content
Jan 29, 2012 07:09 AM|LINK
Hi
For this question, you need to know what is the viewstate, and page life cycle.
And I think the control's value had been auto saved in your viewstate.
If you dynamic create usercontrol before page_load, the view state will auto
used for the control, when post back.
for example:
protected void Page_Load(object sender, EventArgs e) { Table tb = new Table(); TableRow tbrow = new TableRow(); TextBox textbox1 = new TextBox(); TableCell tbcell = new TableCell(); form1.Controls.Add(tb); tbrow.Cells.Add(tbcell); tb.Rows.Add(tbrow); tbcell.Controls.Add(textbox1); form1.Controls.Add(new Button()); } protected void Page_PreRender(object sender, EventArgs e) { }in this program, if you dynamic create the control in page_load, then you click the button, the message in textbox still remain. When you put the code in PreRender, then the message will disappear.
But if you want to store the message to viewstate by this: viewstate["control1"]=textbox1.text, I think you need to specific the textbox id and reload it after dynamic created.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
b471code3
Star
13877 Points
2598 Posts
Re: Dynamic table content
Jan 30, 2012 12:33 PM|LINK
I understand its a state management forum and the links I provided talk about state management if you read the documents. Dynamic controls should be created in Page.Init as ViewState is not available until Page_Load.
SaveViewState() LoadViewState()
So if you read the initial documents I posted and the 2 documents above you will see the importance of ViewState and when it is loaded and how to save data to it. If your controls are created on Page.Init and you save the values to ViewState on Page.Load then on Page.PreRender you can gather those values from ViewState and assign them to your controls. SaveViewState and LoadViewState have arrays of objects. So you access them array index when you save or look for objects to get values from.