I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 219, comments - 2287, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

ASP.NET 2.0 Master page and child pages viewstate inheritance

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.

Print | posted on Friday, September 29, 2006 6:03 PM | Filed Under [ .Net ]

Feedback

Gravatar

# re: ASP.NET 2.0 Master page and child pages viewstate inheritance

The page viewstate and the control viewstate are not two different things -- they're the same; the page viewstate contains all of the data from any controls on that page that have a viewstate. If the page has no viewstate, then NOTHING on the page -- controls or anything else -- will have a viewstate. It is completely disabled for the page.

If you want only certain controls on your page to have a viewstate, you set the page viewstate on and set the viewstate for each control individually.

It seems like you might be thinking that the page's ViewState setting is like a "default viewstate setting" for all of the controls, but it's not. It simple says "collect the controls' viewstate and store them on this page, or don't."


9/29/2006 6:19 PM | Jeff S
Gravatar

# re: ASP.NET 2.0 Master page and child pages viewstate inheritance

Oh i understand the whole concept of page and control viewstates
But still.. it would be nice wouldn't it :)
9/29/2006 6:24 PM | Mladen
Gravatar

# re: ASP.NET 2.0 Master page and child pages viewstate inheritance

Hello!

I would to add, that there's a new mechanism in ASP.NET 2.0 that allows you to persist control state (aptly named ControlState) - regardless of the ViewState setting on the Page or Control in question.

The idea is that control developers (being 3rd party or people on your team) can use a framework that guarantees state persistence on postbacks, regardless of the local or global ViewState state.

Please see the following links for additional information:

http://weblogs.asp.net/dneimke/archive/2005/01/18/354595.aspx

http://fredrik.nsquared2.com/viewpost.aspx?PostID=265

Hopefully this should clear things up a little bit.

With regards
Anders Borum / SphereWorks
Microsoft Certified Developer
10/8/2006 7:15 PM | Anders Borum
Gravatar

# re: ASP.NET 2.0 Master page and child pages viewstate inheritance

Thanx Anders.
That was an informative read.
10/8/2006 8:14 PM | Mladen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET