I am adding controls dynamically in my webpage. I add them in onload method. everything is working fine..
But I m a bit confused about how it works.. I have read in so many articles that all controls get t开发者_如何学Cheir values from viewstate before load event. Then how my dynamically added controls get their values when i am adding them in OnLoad event ie after LoadPostData event.
Load them in Page_Init()
Review the page lifecycle for more info:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
This is a helpful article as well:
http://www.code-magazine.com/article.aspx?quickid=0305101&page=2
In a typical GET request, the controls are created at Page_Init
. Since these dynamic controls are not part of page markup, so in POST BACK, you need to recreate. Be sure that when recreating, it must have same ID otherwise your events/values will not preserve.
Dynamically added controls play catch up in the control lifecycle. Even if you add a control after it has missed the LoadViewState event, that event will still occur for the control at the time when the control is added to the page. I would suggest that any poor souls who have not had the privilege of reading this article, do so immediately:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
Source: Professional programmer, I do not claim to be an expert, but I have read tons of articles about ViewState. :)
精彩评论