View state

Discussion in 'ASP .Net' started by Neven Klofutar, Nov 15, 2005.

  1. Hi,

    I recently read an article on MSDN about viewstate and postback.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

    The author states the following:
    "It is a common misconception among developers that view state is somehow
    responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web
    controls remember their values across postback. This is not the case, as the
    values are identified via posted back form field values, and assigned in the
    LoadPostData() method for those controls that implement
    IPostBackDataHandler."

    I would like to know exactly what ASP.NET controls don't need viewstate to
    remeber value across postbacks.

    thanx, Neven
    Neven Klofutar, Nov 15, 2005
    #1
    1. Advertising

  2. Neven Klofutar

    Karl Seguin Guest

    Any control who's value can be deduced from Request.Form.

    However, note that even in the most obvious case, such as a textbox, the
    viewstate still preserves the OLD value, which is the ONLY way for
    TextChanged event to fire server-side.

    Also, only the _selected_ value is preserved without viewstate. This means
    if you have a radiobutton list with out viewstate, not only will your
    Changed event not fire, but you won't have access to the non-selected radio
    buttons.

    In other words, all form elements can make due with only REquest.Form...if
    you've programmed in classic ASP/PHP, the meaning of this should be
    intuitive.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/

    "Neven Klofutar" <neven.klofutar@**re...m.o..v...e**vip.hr> wrote in message
    news:...
    > Hi,
    >
    > I recently read an article on MSDN about viewstate and postback.
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
    >
    > The author states the following:
    > "It is a common misconception among developers that view state is somehow
    > responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web
    > controls remember their values across postback. This is not the case, as
    > the values are identified via posted back form field values, and assigned
    > in the LoadPostData() method for those controls that implement
    > IPostBackDataHandler."
    >
    > I would like to know exactly what ASP.NET controls don't need viewstate to
    > remeber value across postbacks.
    >
    > thanx, Neven
    >
    >
    Karl Seguin, Nov 15, 2005
    #2
    1. Advertising

  3. I see what you mean Karl, thanx !

    Neven


    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > Any control who's value can be deduced from Request.Form.
    >
    > However, note that even in the most obvious case, such as a textbox, the
    > viewstate still preserves the OLD value, which is the ONLY way for
    > TextChanged event to fire server-side.
    >
    > Also, only the _selected_ value is preserved without viewstate. This
    > means if you have a radiobutton list with out viewstate, not only will
    > your Changed event not fire, but you won't have access to the non-selected
    > radio buttons.
    >
    > In other words, all form elements can make due with only REquest.Form...if
    > you've programmed in classic ASP/PHP, the meaning of this should be
    > intuitive.
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    > "Neven Klofutar" <neven.klofutar@**re...m.o..v...e**vip.hr> wrote in
    > message news:...
    >> Hi,
    >>
    >> I recently read an article on MSDN about viewstate and postback.
    >> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
    >>
    >> The author states the following:
    >> "It is a common misconception among developers that view state is somehow
    >> responsible for having TextBoxes, CheckBoxes, DropDownLists, and other
    >> Web controls remember their values across postback. This is not the case,
    >> as the values are identified via posted back form field values, and
    >> assigned in the LoadPostData() method for those controls that implement
    >> IPostBackDataHandler."
    >>
    >> I would like to know exactly what ASP.NET controls don't need viewstate
    >> to remeber value across postbacks.
    >>
    >> thanx, Neven
    >>
    >>

    >
    >
    Neven Klofutar, Nov 16, 2005
    #3
  4. Neven Klofutar

    Karl Seguin Guest

    In an email follow up, Neven asked me if I could be more clear since he was
    still seeing the TextChanged event fire despite turning off viewstate. Here
    was my response, in case it helps others:

    The TextChanged event is broken under special conditions, I wasn't

    very clear about this.



    Create a simple page:

    <form id="form" method="post" runat="server">

    <asp:Textbox ID="name" Runat="server" EnableViewState="False" />

    <asp:Button ID="x" Runat="server" />

    </form>



    with the following codebehind:

    protected TextBox name;

    protected Button x;

    private void Page_Load(object sender, EventArgs e)

    {

    name.TextChanged += new EventHandler(this.name_TextChanged);

    x.Click += new EventHandler(this.x_Click);

    }

    private void name_TextChanged(object sender, EventArgs e)

    {

    Trace.Write("TextChanged");

    }

    private void x_Click(object sender, EventArgs e)

    {

    Trace.Write("ButtonClicked");

    }



    Enter text and click on the button, "TextChanged" WILL fire. However, now

    CLEAR the text from the textbox and hit the button, "TextChange" WILL NOT

    fire. Because the old value isn't preserved in viewstate, ASP.Net assumes

    that the "old" value is blank. That means that textChanged won't fire if

    you go from a value to a blank one (even though the value has changed).

    Also, if you enter text, say "aa" and hit the button, the hit the button

    again, you'll see that TextChanged fires. That's because it's comparing the

    2nd "aa" to blank, instead of the old value which is actually also "aa".

    Hope that makes some sense :)



    The problem with the radio button list is for code like this:

    private void Page_Load(object sender, EventArgs e)

    {

    if (!Page.IsPostback)

    {

    rad.DataSource = "XX";

    rad.DataBind();

    }

    x.Click += new EventHandler(this.x_Click);

    }

    private void x_Click(object sender, EventArgs e)

    {

    Trace.Write(rad.Items[0].Value); //won't work unless 0 was selected

    if viewstate is off for radiobutton list

    }



    The problem is that values are only added when it isn't postback

    (!Page.IsPostBack). With viewstate on, no problem because the control is

    re-created from viewstate, with it off however, it isn't recreated, and only

    the selected value can be retrieved via Request.Form("rad"). One solution

    is to re-bind the control, on postback as well (remove the check for

    !PAge.IsPostback). you have to decide what you prefer, fetching the data

    source again (might involve a database hit) or loading the data into

    viewstate? I often prefer to reload the data, especially if it's cached!



    Karl


    --
    MY ASP.Net tutorials
    http://www.openmymind.net/
    http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



    "Neven Klofutar" <neven.klofutar*r*e*mo*ve*@vip.hr> wrote in message
    news:...
    >I see what you mean Karl, thanx !
    >
    > Neven
    >
    >
    > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    > wrote in message news:...
    >> Any control who's value can be deduced from Request.Form.
    >>
    >> However, note that even in the most obvious case, such as a textbox, the
    >> viewstate still preserves the OLD value, which is the ONLY way for
    >> TextChanged event to fire server-side.
    >>
    >> Also, only the _selected_ value is preserved without viewstate. This
    >> means if you have a radiobutton list with out viewstate, not only will
    >> your Changed event not fire, but you won't have access to the
    >> non-selected radio buttons.
    >>
    >> In other words, all form elements can make due with only
    >> REquest.Form...if you've programmed in classic ASP/PHP, the meaning of
    >> this should be intuitive.
    >>
    >> Karl
    >>
    >> --
    >> MY ASP.Net tutorials
    >> http://www.openmymind.net/
    >>
    >> "Neven Klofutar" <neven.klofutar@**re...m.o..v...e**vip.hr> wrote in
    >> message news:...
    >>> Hi,
    >>>
    >>> I recently read an article on MSDN about viewstate and postback.
    >>> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
    >>>
    >>> The author states the following:
    >>> "It is a common misconception among developers that view state is
    >>> somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and
    >>> other Web controls remember their values across postback. This is not
    >>> the case, as the values are identified via posted back form field
    >>> values, and assigned in the LoadPostData() method for those controls
    >>> that implement IPostBackDataHandler."
    >>>
    >>> I would like to know exactly what ASP.NET controls don't need viewstate
    >>> to remeber value across postbacks.
    >>>
    >>> thanx, Neven
    >>>
    >>>

    >>
    >>

    >
    >
    Karl Seguin, Nov 18, 2005
    #4
    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. VB Programmer
    Replies:
    1
    Views:
    584
    VB Programmer
    Jul 10, 2003
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Icon View and list View in ASP.NET

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Mar 7, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    484
    =?Utf-8?B?UmFlZCBTYXdhbGhh?=
    Mar 7, 2005
  3. Luqman
    Replies:
    2
    Views:
    3,796
    Luqman
    Feb 1, 2006
  4. Jimmy
    Replies:
    0
    Views:
    497
    Jimmy
    Mar 15, 2007
  5. Parthiv Joshi
    Replies:
    1
    Views:
    649
    Samuel L Matzen
    Jul 6, 2004
Loading...

Share This Page