god damn ViewState.

Discussion in 'ASP .Net' started by Lukasz Lacki, Nov 14, 2003.

  1. Lukasz Lacki

    Lukasz Lacki Guest

    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
    Lukasz Lacki, Nov 14, 2003
    #1
    1. Advertising

  2. ViewState options

    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
    Peter O'Reilly, Nov 14, 2003
    #2
    1. Advertising

  3. Lukasz Lacki

    Brian W Guest

    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" <-spam> wrote in message
    news:...
    > 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
    >
    Brian W, Nov 14, 2003
    #3
  4. Lukasz Lacki

    Lukasz Lacki Guest

    Re: ViewState options

    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;
    <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


    }
    }
    Lukasz Lacki, Nov 14, 2003
    #4
  5. Lukasz Lacki

    L. L. Guest

    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.

    "Lukasz Lacki" <-spam> wrote in message
    news:...
    > 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
    >
    L. L., Nov 14, 2003
    #5
  6. Lukasz Lacki

    Lukasz Lacki Guest

    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;
    <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.
    Lukasz Lacki, Nov 14, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Admin
    Replies:
    0
    Views:
    655
    Admin
    Jul 8, 2003
  2. Neo Geshel
    Replies:
    2
    Views:
    3,573
    Versteijn
    Aug 18, 2004
  3. The Clansman

    help me with this damn problem!!!!!!!

    The Clansman, Aug 8, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    400
    =?Utf-8?B?QWxleGFuZGVy?=
    Sep 29, 2004
  4. Kev

    Damn CS0006 error

    Kev, Jun 27, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    386
  5. jean-marc
    Replies:
    0
    Views:
    462
    jean-marc
    May 19, 2005
Loading...

Share This Page