I had so much "fun" with viewstate last weekend.
I'm developing a site that has a master page Master.aspx and a few child pages (Page1.aspx, ..., pageN.aspx).
Now when rendering HTML that is displayed an ASP.NE rendering engine first reads the child page
then takes the master page and incorporates it's contorls into the child page control hierarchy so the
child page actually becomes the overall master. A master page is treated as a user control, after all it
is derived from UserControl class. All this happens on the pre Init part of the Page creation.
So now comes in the viewstate. I won't be going into what viewstate is because i'm assuming you're familiar vith it.
However if you're not i'd go and read this article.
Now ASP.Net 2.0 also has a control viewstate along with the page viewstate.
This is good because you can disable the viewstate on specific controls and they won't get viewstated :)
So if you enable a page's viewstate and disable it on some controls, those controls won't be viewstated.
That's great, if you ask me.
Naturaly i thought it works in the other way too. Man was I wrong. :(
My specific scenario is this:
I have a master page on which i have a TextBox, a DropDownList and a Button. It's a standard search thingie.
type in a search text choose a search option (all words, exact phrase, etc) press the button and voilá.
The search results would be shown on the child page.
Only on the button's OnClick server event i couldn't get the selected value of the DropDownList or the text of the TextBox.
I checked the viewstate of the master page it was enabled and so was on the search involved controls.
I never even thought to look at the child page which had it's viewstate disabled.
Enabling it did the trick and i could happily search my database.
But a question arises:
If i can enable a page viewstate and disable the contorl viewstate,
WHY CAN'T I DO THE SAME THING IN REVERSE????
I want to be able to disable the page viewstate and enable certain controls' viewstates and
those controls would be viewstated.
Am i the only one to whom this would make sense?
There's probably a reason for this and if someone can shed some light i'd be gratefull.