Bound Data, ViewState, and PostBack

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
7/23/2010 1:39:55 AM
Gravatar
Total Posts 156

Bound Data, ViewState, and PostBack

So I have enabled the ViewState like so:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new EventHandler(Page_Load);
this.EnableViewState = true;
}

 

 

I bind the data to a bunch of dropdowns/lists like so:

// Bind on the first load, let ViewState persist the data from here on
if (!this.IsPostBack)
{

using (IDataReader reader = OnlineGiving.ListOfStates())
{
drpState.DataSource = reader;
drpState.DataTextField = "Stat_Val";
drpState.DataValueField = "Stat_Code";
drpState.DataBind();
}
using (IDataReader reader = OnlineGiving.ListOfCountries())
{
drpCountry.DataSource = reader;
drpCountry.DataTextField = "Ctry_Val";
drpCountry.DataValueField = "Ctry_Code";
drpCountry.DataBind();
}


using (IDataReader reader = OnlineGiving.ListOfDivisions())
{
lstDivisions.DataSource = reader;
lstDivisions.DataTextField = "Div_Val";
lstDivisions.DataValueField = "Div_Code";
lstDivisions.DataBind();
}
 

However, when the postback hits, these controls are not re-populated using ViewState - they are blank.  What am I missing?

7/23/2010 7:43:27 AM
Gravatar
Total Posts 18439

Re: Bound Data, ViewState, and PostBack

instead of this.EnableViewState = true; try Page.EneableViewState = true;

Hope it helps,

Joe

7/23/2010 4:27:13 PM
Gravatar
Total Posts 156

Re: Bound Data, ViewState, and PostBack

That helped - thanks.  Wonder why this didn't work :/

8/20/2010 4:48:50 PM
Gravatar
Total Posts 156

Re: Bound Data, ViewState, and PostBack

Never mind, it didn't - I'm still getting plenty of viewstate lines and I'm not sure why.  If the page's EnableViewState setting is set to false, then any child elements's settings are disregarded even if they are set to true in .Net 3.5, so this doesn't make sense.  I have ViewState disabled at the page level.  I even set the ViewStateMode to Disabled on  <portal:mojoPanel ID="mp1" runat="server" ArtisteerCssClass="art-Post"
RenderArtisteerBlockContentDivs="true" ViewStateMode="Disabled"> since I run .Net 4.  Yet, I still get a bunch of ViewState lines and I can't figure out where they are coming from:

 

<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATEFIELDCOUNT" id="__VIEWSTATEFIELDCOUNT" value="29" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjA1OTAyNzk1MQ9kFgJmD2QWAgIDD2QWDgIDDxYCHg9TaXRlTWFwUHJvdmlkZXIFCW1vam9zaXRlMWQCCw8PFgIeB1Zp" />
<input type="hidden" name="__VIEWSTATE1" id="__VIEWSTATE1" value="c2libGVoZBYMAgEPDxYCHwFoZGQCAw8PFgIfAWhkZAIFDw8WAh8BaGRkAgcPDxYCHwFoZGQCCQ8PFgIfAWhkZAILDw8WAh8BaGRk" />
<input type="hidden" name="__VIEWSTATE2" id="__VIEWSTATE2" value="AhEPDxYEHghDc3NDbGFzcwUTYWx0Y29udGVudDEgY21zem9uZR4EXyFTQgICZBYCAgEPZBYEAgEPZBYCZg8PFgQfAgUIbW9kdWxl" />
<input type="hidden" name="__VIEWSTATE3" id="__VIEWSTATE3" value="MTMfAwICZBYCAgEPZBYCAgMPZBYCAgMPZBYCAgEPDxYEHgtDb250ZW50R3VpZCgpWFN5c3RlbS5HdWlkLCBtc2NvcmxpYiwgVmVy" />
<input type="hidden" name="__VIEWSTATE4" id="__VIEWSTATE4" value="c2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkkZWE2NTRjYTktYTAy" />
<input type="hidden" name="__VIEWSTATE5" id="__VIEWSTATE5" value="MS00OGRhLThjZTMtYWIzMDY5YzRlNDA1HwFoZBYCAgEPZBYCZg9kFgQCBQ9kFgJmDxYCHgpDYWxsYmFja0lEBSljdGwwMSRhbHRD" />
<input type="hidden" name="__VIEWSTATE6" id="__VIEWSTATE6" value="b250ZW50MSRjdGwwMCRSYXRpbmckVXNlclJhdGluZ2QCBw8WAh4FVmFsdWUFJGVhNjU0Y2E5LWEwMjEtNDhkYS04Y2UzLWFiMzA2" />
<input type="hidden" name="__VIEWSTATE7" id="__VIEWSTATE7" value="OWM0ZTQwNWQCAg9kFgJmDw8WBB8CBQhtb2R1bGUxMh8DAgJkFgICAQ9kFgICAw9kFgICAw9kFgICAQ8PFgQfBCgrBCQ2NGI1MTMy" />

<input type="hidden" name="__VIEWSTATE8" id="__VIEWSTATE8" value="MC03MzgxLTQzZWItOWEyYi1lZGQyZWZlYjZiMjEfAWhkFgICAQ9kFgJmD2QWBAIFD2QWAmYPFgIfBQUpY3RsMDEkYWx0Q29udGVu" />
<input type="hidden" name="__VIEWSTATE9" id="__VIEWSTATE9" value="dDEkY3RsMDEkUmF0aW5nJFVzZXJSYXRpbmdkAgcPFgIfBgUkNjRiNTEzMjAtNzM4MS00M2ViLTlhMmItZWRkMmVmZWI2YjIxZAIT" />
<input type="hidden" name="__VIEWSTATE10" id="__VIEWSTATE10" value="Dw8WBB8CBTlhcnQtbGF5b3V0LWNlbGwgYXJ0LXNpZGViYXIxIGxlZnRzaWRlIGxlZnQyY29sdW1uIGNtc3pvbmUfAwICZBYCAgEP" />
<input type="hidden" name="__VIEWSTATE11" id="__VIEWSTATE11" value="ZBYCZg9kFgJmDw8WBB8CBQhtb2R1bGUxNB8DAgJkFgICAQ9kFgICAw9kFgICAw9kFgICAQ8PFgQfBCgrBCQwOWJkMTk2NS03NDU5" />
<input type="hidden" name="__VIEWSTATE12" id="__VIEWSTATE12" value="LTQzYjMtYTVlMi1kYWE1ZTJiNjg0ZDcfAWhkFgICAQ9kFgJmD2QWBAIFD2QWAmYPFgIfBQUpY3RsMDEkbGVmdENvbnRlbnQkY3Rs" />
<input type="hidden" name="__VIEWSTATE13" id="__VIEWSTATE13" value="MDAkUmF0aW5nJFVzZXJSYXRpbmdkAgcPFgIfBgUkMDliZDE5NjUtNzQ1OS00M2IzLWE1ZTItZGFhNWUyYjY4NGQ3ZAIVDw8WBB8C" />
<input type="hidden" name="__VIEWSTATE14" id="__VIEWSTATE14" value="BTVhcnQtbGF5b3V0LWNlbGwgYXJ0LWNvbnRlbnQgY2VudGVyLWxlZnRtYXJnaW4gY21zem9uZR8DAgJkZAIXDw8WBh8CBRFyaWdo" />
<input type="hidden" name="__VIEWSTATE15" id="__VIEWSTATE15" value="dHNpZGUgY21zem9uZR8DAgIfAWhkZAIbDw8WAh8BaGRkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYXBSVjdGww" />
<input type="hidden" name="__VIEWSTATE16" id="__VIEWSTATE16" value="MSRtYWluQ29udGVudCRjdGwwMCRyZGJOZWVkZWRNb3N0BSVjdGwwMSRtYWluQ29udGVudCRjdGwwMCRyZGJOZWVkZWRNb3N0BSFj" />
<input type="hidden" name="__VIEWSTATE17" id="__VIEWSTATE17" value="dGwwMSRtYWluQ29udGVudCRjdGwwMCRyZGJDaG9vc2UFIWN0bDAxJG1haW5Db250ZW50JGN0bDAwJHJkYkNob29zZQUlY3RsMDEk" />
<input type="hidden" name="__VIEWSTATE18" id="__VIEWSTATE18" value="bWFpbkNvbnRlbnQkY3RsMDAkbHN0QWNjb3VudHMkMAUkY3RsMDEkbWFpbkNvbnRlbnQkY3RsMDAkY2hrTWF0Y2hHaWZ0BShjdGww" />
<input type="hidden" name="__VIEWSTATE19" id="__VIEWSTATE19" value="MSRtYWluQ29udGVudCRjdGwwMCRjaGtDb3Jwb3JhdGVHaWZ0BS1jdGwwMSRtYWluQ29udGVudCRjdGwwMCRjaGtIb25vck1lbW9y" />
<input type="hidden" name="__VIEWSTATE20" id="__VIEWSTATE20" value="eVNvbWVvbmUFKGN0bDAxJG1haW5Db250ZW50JGN0bDAwJGNoa1BsZWRnZVBheW1lbnQFKGN0bDAxJG1haW5Db250ZW50JGN0bDAw" />
<input type="hidden" name="__VIEWSTATE21" id="__VIEWSTATE21" value="JGNoa0Fub255bW91c0dpZnQFMGN0bDAxJG1haW5Db250ZW50JGN0bDAwJGNoa0FkZGl0aW9uYWxJbmZvcm1hdGlvbgUuY3RsMDEk" />
<input type="hidden" name="__VIEWSTATE22" id="__VIEWSTATE22" value="bWFpbkNvbnRlbnQkY3RsMDAkY2hrSGF2ZUFueUFmZmlsaWF0aW9ucwUkY3RsMDEkbWFpbkNvbnRlbnQkY3RsMDAkY2hrRmlyc3RH" />
<input type="hidden" name="__VIEWSTATE23" id="__VIEWSTATE23" value="aWZ0BSJjdGwwMSRtYWluQ29udGVudCRjdGwwMCRjaGtBbHVtbnVzBShjdGwwMSRtYWluQ29udGVudCRjdGwwMCRjaGtTcG91c2VQ" />
<input type="hidden" name="__VIEWSTATE24" id="__VIEWSTATE24" value="YXJ0bmVyBSRjdGwwMSRtYWluQ29udGVudCRjdGwwMCRjaGtOSVVGcmllbmQFJWN0bDAxJG1haW5Db250ZW50JGN0bDAwJGNoa05J" />

<input type="hidden" name="__VIEWSTATE25" id="__VIEWSTATE25" value="VVN0dWRlbnQFK2N0bDAxJG1haW5Db250ZW50JGN0bDAwJGNoa05JVVN0dWRlbnRQYXJlbnQFLWN0bDAxJG1haW5Db250ZW50JGN0" />
<input type="hidden" name="__VIEWSTATE26" id="__VIEWSTATE26" value="bDAwJGNoa0ZhY3VsdHlTdGFmZk1lbWJlcgUjY3RsMDEkbWFpbkNvbnRlbnQkY3RsMDAkcmRiTGFuZExpbmUFI2N0bDAxJG1haW5D" />
<input type="hidden" name="__VIEWSTATE27" id="__VIEWSTATE27" value="b250ZW50JGN0bDAwJHJkYkxhbmRMaW5lBR9jdGwwMSRtYWluQ29udGVudCRjdGwwMCRyZGJDZWxsBR9jdGwwMSRtYWluQ29udGVu" />
<input type="hidden" name="__VIEWSTATE28" id="__VIEWSTATE28" value="dCRjdGwwMCRyZGJDZWxsBSdjdGwwMSRtYWluQ29udGVudCRjdGwwMCRsc3REZXNpZ25hdGlvbnMPZ2Q=" />
</div>
 

8/20/2010 5:11:03 PM
Gravatar
Total Posts 18439

Re: Bound Data, ViewState, and PostBack

Hi,

When I first read about the differences in viewstate in .NET 4, I thought like you that it just changed the behavior of EnableViewstate, but it is a little more complicated than that.

Really to minimize viewstate in .NET 4 you need to set EnableViewstate to true on the page, but then set a new property (on the page) ViewStateMode to ViewStateMode.Disabled

this will turn off viewstate for all controls on the page and it can only be turned back on by setting the ViewStateMode property of the child control to ViewStateMode.Enabled. By default, controls have ViewStateMode.Inherit which is why turning it off at the page turns it off in .NET 4.

I guess they did it this way for backward compatibility to avoid breaking things that were built for .NET 2 or 3.5.

Hope it helps,

Joe

8/20/2010 5:46:56 PM
Gravatar
Total Posts 156

Re: Bound Data, ViewState, and PostBack

Did like you said - still getting around 14 lines of ViewState.

However, what you said doesn't make sense to me.  I referred to the following article: http://msdn.microsoft.com/en-us/magazine/ee431529.aspx

8/20/2010 6:03:13 PM
Gravatar
Total Posts 18439

Re: Bound Data, ViewState, and PostBack

on the <pages element in your Web.config, make sure you have controlRenderingCompatibilityVersion="4.0"

Hope it helps,

Joe

8/20/2010 6:04:50 PM
Gravatar
Total Posts 156

Re: Bound Data, ViewState, and PostBack

Also, when I set  ClientIDMode to "Static" for the top level module wrapper like so: <portal:mojoPanel ID="mp1" runat="server" ArtisteerCssClass="art-Post"
RenderArtisteerBlockContentDivs="true"  ClientIDMode="Static">

It doesn't work either.  It' ID is still auto-generated.  In .Net 4 ClientIDMode is set to Inherit, so all child controls within the wrapper were supposed to use the ID I specified manually. 

So, thus far - I haven't been able to get either of these new .Net features to work.  I checked the Projects properties for both my Web.UI project for mojoPortal.Web and they are both set to run .Net 4, so I'm not sure what the heck is going on.

controlRenderingCompatibilityVersion is set to "4.0" 

8/20/2010 6:16:53 PM
Gravatar
Total Posts 18439

Re: Bound Data, ViewState, and PostBack

If you're expecting viewstate to drop to zero you may be expecting too much, there could be controls in the page from asp.net or the ajaxtoolkit or elsewhere that opt in to viewstate and nothing you can do is going to change that behavior of a control.

Best way to find where it is coming from is to chop things out of the page and see what removes it.

I can say that in the SiteSettings page and a few other pages I was able to reduce viewstate by 50% under .NET 4, though these were and still are heavy pages because they have a lot of controls for a lot of settings.

I did it inside conditional compilation since I still have to support .NET 3.5

Hope it helps,

Joe

8/20/2010 6:24:44 PM
Gravatar
Total Posts 156

Re: Bound Data, ViewState, and PostBack

Home page gets about 13 lines of ViewState whereas my page gets 32 and I have it disabled at the page level - that's just insane.  Something is not right.  Any ideas as to why ClientIDMode isn't working though? Is it because you have ClientIDMode="AutoID" in web.config?

 

 Also, why use auto-generated IDs?  They are so long..

8/21/2010 6:26:30 AM
Gravatar
Total Posts 18439

Re: Bound Data, ViewState, and PostBack

regarding viewstate, if there is a line of code in any of your controls that sets Page.EnableViewstate = false; it will cause it to behave like 3.5 .NET and ViewStateMode settings will have no effect.

regarding ClientIdMode, it would be dangerous for me to make a global change to static it could break things. For the time being while we are still supporting 3.5 .NET I also cannot go sprinkling ClientIdMode properties on control declarations since the same files are used in 3.5 .NET. So myself I'm waiting until we drop support for 3.5 .NET before I start trying to change the way client ids are rendered.

However, that setting in Web.config should not prevent you from specifying ClientIdMode = static or predictable on your own custom controls.

Hope it helps,

Joe

8/21/2010 6:51:24 AM
Gravatar
Total Posts 18439

Re: Bound Data, ViewState, and PostBack

Here is a better article about viewstate in .NET 4

http://www.4guysfromrolla.com/articles/071410-1.aspx

note especially the little section about if EnableViewState and ViewStateMode got in a fight who would win

Best,

Joe

You must sign in to post in the forums. This thread is closed to new posts.