Viewstate/Postback for dynamic controls. CRAZY?

M

mivhaelveloz

I thought I had a good grip on viewstate handling, but am suprised by a
small test:

In my aspx file I have a PlaceHolder control and a button for posting
back.

Dynamically in Page_Load I create a TextBox, and add it as a control of
PlaceHolder.

I do this regardless of whether IsPostBack is true or not, to insure
the control always exists on the page.

My expectation would be that any user entered information in the
TextBox would not be available across postbacks because I am not
creating it until Page_Load, which supposedly occurs *after* controls
have loaded their viewstate and processed any postback data.

But, surprise! The information I enter into the text box is being
acquired by it somehow, as I see it show up in the test box after
clicking the button, which causes a postback.

How can my textbox be getting the user supplied data if it's created
*after* the postback data has already been processed??

... To expand the test a bit, I also modified a property of the text box
in Page_Load, but only when IsPostBack == false. In other words, I
"initialize" the TextBox's Columns property to 40. Again, my
expectation is that during postback the textbox wouldn't retain this
setting as I recreate the control after viewstate handling has occured.
But yet it retains the setting.

WHAT GIVES???????

Thanks for any insights!
 
W

Winista

if you add any control at run time, the framework makessure that it gets
loaded completely during Page_Load and its view state has been loaded too.
Page_Load makes sure that the control tree has been fully created the you
want it to be and then it moves to nest step. And thats what you are seeing.
when you called LoadControl or AddControl in Page_Load, the framework gave
this control chance to play catch up and its view state got loaded into the
control.
 
M

mivhaelveloz

Thanks, that's definitely what I'm seeing.. guess I was just surprised
because I've seen other articles on the web about how if you want
dynamic controls to partake in viewstate that they must be created in
Page_Init because Page_Load is too late. Maybe this was changed
sometime later and the articles I read were outdated...???

Thanks for the reply
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top