0

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 their 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.

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.