god damn ViewState.

  • Thread starter Thread starter Lukasz Lacki
  • Start date Start date
L

Lukasz Lacki

i want to disable ViewState in dynamicly created control (in code).

below, sample code that DOESN'T work (but should in my opinion).
placeHolder is an control created in editor and placed on form.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder placeHolder;

private void Page_Load(object sender, System.EventArgs e)
{
placeHolder.EnableViewState = false;

TextBox tb = new TextBox();
tb.ID = "TBID";
tb.EnableViewState = false;
tb.Text = "random" + new
Random(unchecked((int)DateTime.Now.Ticks)).Next();
placeHolder.Controls.Add(tb);

Button b = new Button();
b.Text = "Reload";
placeHolder.Controls.Add(b);
}
}

i disabled viewstate in TextBox (tb.EnableViewState = false;), but ASP
still restores value generated in first post.

how can i go around that mess?

i've tried to create inherited class from TextBox that overrides
LoadViewState and SaveViewState, doing nothing, but god damn asp engine,
still (how!?) restores value.

changing value (new random number) in PreRender of some event handler is
not an option for me.

please help. thx
 
Try the @Page directive's enableviewstate attribute to disable it for all or
the Init event of the Page to selectively disable view state.
 
Peter said:
Try the @Page directive's enableviewstate attribute to disable it for all or
the Init event of the Page to selectively disable view state.

doesn't work :(

whole code for the form

aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="Test2.WebForm1" EnableViewState="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
&nbsp;
<asp:PlaceHolder id="placeHolder" runat="server"></asp:PlaceHolder>
</form>
</body>
</HTML>

---------------------------------------
code:
---------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Test2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder placeHolder;

private void Page_Load(object sender, System.EventArgs e)
{
placeHolder.EnableViewState = false;
TextBox tb = new TextBox();
tb.ID = "VARID";
tb.EnableViewState = false;
tb.Text = "random" + new
Random(unchecked((int)DateTime.Now.Ticks)).Next();
placeHolder.Controls.Add(tb);

Button b = new Button();
b.Text = "Reload";
placeHolder.Controls.Add(b);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


}
}
 
Please refer to this artcicle
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp11222001.asp
In this article it says "ViewState is enabled by default, and it's up to
each control-not the page developer-to decide what gets stored in ViewState.
".

My understanding is even though you set the property of EnableViewState of
textbox to false, the textbox still persists the text value. Because it does
not increase the page size significantlyby doing so. However, for controls
like datagrid, it indeed decrease the size of the page.
Also if EnableViewState is set to false on page level, assigning values to
ViewState will not persist. For example, if you do ViewState["key1"] =
"value1"; in page load. And then try to retrieve ViewState["key1"] on
postback, you will get null.

L.L.
 
Brian said:
I think you have a misconception of what vViewState is Take a look at the
following articles by Paul Wilson, they may just explain things a little
better

http://www.ASPAlliance.com/PaulWilson/Articles/?id=7
http://www.ASPAlliance.com/PaulWilson/Articles/?id=6
http://www.WilsonDotNet.com/Demos/ViewState.aspx

HTH
Brian W

i hope you're right. thx for articles.

but if you run this simple code below, you will admit that "something"
is not allright with viewstate in ASP .NET

----------------------------------------------

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="Test2.WebForm1" EnableViewState="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
&nbsp;
<asp:PlaceHolder id="placeHolder" runat="server"></asp:PlaceHolder>
<asp:TextBox id="tbTest" style="Z-INDEX: 101; LEFT: 38px; POSITION:
absolute; TOP: 56px" runat="server" EnableViewState="False"></asp:TextBox>
</form>
</body>
</HTML>

--------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Test2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbTest;
protected System.Web.UI.WebControls.PlaceHolder placeHolder;

private void Page_Load(object sender, System.EventArgs e)
{
placeHolder.EnableViewState = false;
TextBox tb = new TextBox();
tb.ID = "VARID";
tb.EnableViewState = false;
int rnd = new Random(unchecked((int)DateTime.Now.Ticks)).Next();
tb.Text = "random" + rnd;
placeHolder.Controls.Add(tb);

tbTest.Text = "random" + rnd;

Button b = new Button();
b.Text = "Reload";
placeHolder.Controls.Add(b);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


}
}

----------------------------------------------

sorry of pasting much of content, but it's the easiest way to show,
something is bugy in this asp .net stuff

for me it's amazing.
 
Back
Top