Geeks With Blogs

News

Series

Add to Technorati Favorites


An Archived Managed World This blog has moved to http://www.managed-world.com/blog

Well, I brought this up briefly at the Portland Nerd Dinner last night and got a couple quirky faces so I thought I would make a post about it.

The issue is that even when viewstate is disabled, the Text property of the ASP.NET text box still persists. The confusing part is that the text property for the TextBox saves itself directly into the view state. For example, the code for the Text property of the TextBox looks as follows (this was retrieved by using Anakrino):

public string Text 
{
     get
     {
          string text;

          text = (String)this.ViewState["Text"];
          if (text != null)
               return text;
          return String.Empty;
     }

     set
     {
          this.ViewState["Text"] = value;
     }
}

 

How do we know it is persisting? Well, if you add a Button that posts back the value of the text property is preserved. Not only that, but if you use Fritz Onion's View State Decoder, you can see that the text property is indeed persisted into the view state.

Quite an interesting little puzzle. I may just be overlooking something. Any thoughts?

Posted on Wednesday, December 17, 2003 4:10 PM | Back to top


Comments on this post: ViewState persists with ViewState disabled? What The.....?!?!?!?!

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
There might be overrides on the controls, so even if you turn off the viewstate for the page, the individual control's requirement for viewstate could force the viewstate to be present.
Left by Iain on Jan 20, 2004 3:32 AM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
How about DropDownList? I have enabled the viewstate for the dropdownlist at the design, but however, it seems the value is not preserved after the postback.
Left by Ranjith on Nov 02, 2004 2:26 PM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
I have your logical explanation :)

The values of text boxes and drop down lists are not stored in ViewState. Confusing when you first look at this I know.

To understand this, think about the way classic HTML works for a moment.

How do you access the value of a text box or drop down list in classic HTML?? Request.forms right??

ASP.net does this same thing.

If ASP.net stored the selected value of controls in ViewState then it would be an enormous waste because the value is already available in the Request.forms collection. So ASP.net would actually be passing it twice to the server.

Since the request.forms collection always contains the value, ASP.net simply pulls the value from the request.forms collection and reloads the value in the next post back.

That is why it does not matter if you turn viewstate on or off for a textbox or a dropdown list to reload its value.

For a dropdown list, ViewState only holds the available options...not the selected value.

For a textbox viewstate is not used at all.

hope that helps

Left by Matt on Feb 14, 2005 11:31 AM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
Thanks Matt,

That helps me with my disappearing dropdownlist selected values!

Now my code looks similar to this:
if (Page.IsPostBack){
Reload_DDL_Options();
DDL.SelectedValue = Request.Form["DDL"];
}

and voila!
Left by Michael on Jun 03, 2005 11:50 PM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
Hey Matt, could you enlighten the statement For a textbox viewstate is not used at all. Where it not used? Though I have veiwstate disabled dropdownlist that defined in .aspx page still the selected value retains among postback. It s kind a little clashes with your statements above. Pls could you help me with this did I miss something?
Left by dejawoo on Jul 08, 2005 10:57 PM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
Could you explain the viewstate concept for dropdown list box. Bcoz I am getting all the values of dropdown getting populated during postback.
Left by Manickam on Jul 19, 2005 6:45 AM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
Not so - VALUES in drop down lists ARE persisted between postbacks by viewstate. The Request object only holds the selected value, which is entirely different.
Left by G on Sep 27, 2007 7:18 AM

# re: ViewState persists with ViewState disabled? What The.....?!?!?!?!
Requesting Gravatar...
This is fine. But, how do you disable the viewstate for a textbox to keep a new value from being replaced by the request.form object value.

Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<table style="width:100%;">
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button"/>
</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td colspan="3" id="tdDyn" runat="server">
&nbsp;</td>
</tr>
</table>

</div>
</form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
}

protected override void OnInit(EventArgs e)
{
HtmlInputText txtBox = new HtmlInputText();
String textValue = "";

if (!IsPostBack)
{
Session["tstVal"] = "Start Value";
}

if (!String.IsNullOrEmpty(Request.Form["Button1"]))
{
ViewState["txtBox"] = "TestValue";
Session["tstVal"] = "TestValue";
textValue = Convert.ToString(Request.Form["txtBox"]);
}

txtBox.ID = "txtBox";
txtBox.Value = Convert.ToString(Session["tstVal"]);
tdDyn.Controls.Add(txtBox);

base.OnInit(e);

}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);

ViewState["txtBox"] = "";
}
}
Left by Shawn on Jan 22, 2008 4:13 PM

Your comment:
 (will show your gravatar)


Copyright © Jason Olson | Powered by: GeeksWithBlogs.net