Error in Label ASP.ViewState implementation?
EDIT 2: Well, I went to code. Note what they do here. They say "load viewstate" and then rotate and set the "Text" property to whatever was in the view. After LoadViewState is called, although view mode tracking is enabled and this results in the behavior I see. I think I should say the code:
if (s! = Text) {Text = s;}. This will completely save you the trouble and preserve whatever invariant they need.
/// <internalonly/>
/// <devdoc>
/// <para>Load previously saved state.
/// Overridden to synchronize Text property with LiteralContent.</para>
/// </devdoc>
protected override void LoadViewState(object savedState) {
if (savedState != null) {
base.LoadViewState(savedState);
string s = (string)ViewState["Text"];
if (s != null)
Text = s;
}
}
Edit. Of all my tests, this only affects the Label control. I still think this is a bug.
This is an ASP.NET 3.5 website.
Consider the following .aspx page: (html, head, body, etc.).
<form id="form1" runat="server">
<asp:Label runat="server" ID="label1">
This is a lot of text.
This is a lot of text.
This is a lot of text.
This is a lot of text.
This is a lot of text.
This is a lot of text.
This is a lot of text.
</asp:Label>
<asp:Button runat="server" ID="button1" Text="Click" OnClick="button1_Click" />
<script>
document.write(document.getElementById("__VIEWSTATE").value.length);
</script>
</form>
The page has the following code:
protected void button1_Click(object sender, EventArgs e) {
//label1.AccessKey = "a";
}
Yes, this line is commented out. We come to this. So, when you click the button, you will see the viewstate is 52 bytes. Even though there is a lot of text on the label, of course the way the viewstate works is that it doesn't need to store a lot of text in the view, because the initial value of the Text property has never changed. OK. So far, so good. This is all expected behavior. In fact, even if the label contains 1 megatext of text, the view size will still be 52 bytes. OK. Now change the method to
protected void button1_Click(object sender, EventArgs e) {
label1.AccessKey = "a";
}
It doesn't matter which property we change. Now press the button. The ViewState size reaches 92 bytes. Good 40 bytes to store a single character access key, not much if you ask me, but whatever :) Now press the button again. What is the size of the viewport now? Should be 92 bytes to the right? No. 480. The click is bigger and it stays at 480 bytes. What's happening? Changing the label property caused the label to start storing the TEXT label in the view. What kind???? Glue 100K of text on the label and you can see the viewstate goes up to ~ 100K.
This is mistake? How is this possibly expected behavior?
a source to share
It doesn't just store one symbol, it needs to store the property to which it belongs too.
See http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/truly-understanding-viewstate.aspx for more details on how viewstate works .
While there may be ways in which the viewstate might not behave the way you expect, there are unlikely to be any bugs since the viewstate is central to ASP.NET to work.
a source to share
ASP.NET dev is here :) And the author of the mentioned article is viewstate.
Speaking like me and not like MS, just to be clear. Yes, this is a mistake. To shed some light on why the code sets a property when it would seem to be unnecessary (since the value is already stored in the ViewState - why set it, which will return it to the ViewState again?). If you look at the installer for text, you can see that besides setting the value in the ViewState, it calls Controls.Clear (). This is because Label supports both "text" and literal string child controls. If it didn't set the Text property on LoadViewState, it might not properly display controls instead of text or something.
This is better handled - not sure in my head if the suggested fix you mentioned will work in all scenarios.
a source to share