Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > god damn ViewState.

Reply
Thread Tools

god damn ViewState.

 
 
Lukasz Lacki
Guest
Posts: n/a
 
      11-14-2003
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

 
Reply With Quote
 
 
 
 
Peter O'Reilly
Guest
Posts: n/a
 
      11-14-2003
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 O'Reilly


 
Reply With Quote
 
 
 
 
Brian W
Guest
Posts: n/a
 
      11-14-2003
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



"Lukasz Lacki" <(E-Mail Removed)-spam> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Lukasz Lacki
Guest
Posts: n/a
 
      11-14-2003
Peter O'Reilly wrote:
> 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;
<asplaceHolder id="placeHolder" runat="server"></asplaceHolder>
</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


}
}

 
Reply With Quote
 
L. L.
Guest
Posts: n/a
 
      11-14-2003
Please refer to this artcicle
http://msdn.microsoft.com/library/de...sp11222001.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.

"Lukasz Lacki" <(E-Mail Removed)-spam> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Lukasz Lacki
Guest
Posts: n/a
 
      11-14-2003
Brian W wrote:

> 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;
<asplaceHolder id="placeHolder" runat="server"></asplaceHolder>
<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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
God damn error 666 (Tkinter in PythonWin) jean-marc Python 0 05-19-2005 03:03 AM
Thank God SirReadAlot MCSD 0 01-27-2004 01:32 PM
Oh My God!!! Can't open Web project André Almeida Maldonado ASP .Net 4 01-07-2004 02:01 PM
Oh god, what will I have stared with this post... Brent Eamer Java 25 12-28-2003 10:36 PM
JasperException (god do I hate jsp exceptions) Felix Java 0 08-05-2003 11:10 PM



Advertisments