How to retrieve control's value from ViewState

Discussion in 'ASP .Net' started by epigram, Jun 3, 2005.

  1. epigram

    epigram Guest

    I'm responding to a button click event on an asp.net web form. I then need
    to retrieve the value from a TextBox control and I want to compare it
    against the control's previous value to see if it has changed. How can I
    retrieve a control's previous value from the ViewState? I know that I could
    save the control's previous value in a session variable, reretrieve it's
    value from the db, etc. But it would appear the control's previous value
    would be in the ViewState since that is what ASP.NET uses to fire events
    such as TextChanged.

    Thanks!
    epigram, Jun 3, 2005
    #1
    1. Advertising

  2. Are you asking "how do I pull from viewstate?" ViewState is a statebag
    object, which has key/value pairs. To pull from viewstate, you can pull
    directly from ViewState:

    //direct pull
    string value = (string) ViewState["keyName"];

    The key here is a) pulling the correct value and b) pulling at the right
    time (before it is reset). This is rather easy, as the order is (i.e., are
    you asking "when can I pull from Viewstate?"):

    Page.Init
    Page.LoadViewState
    Page.ProcessPostData
    Page.Load
    Change events for controls
    Server side validation
    Button.Click or Button.Command events
    Page.PreRender
    Page.SaveViewState <<< This is where it is too late to pull the old value
    Page.Render

    As long as you grab the old value before Page.SaveViewState, you are fine.

    As a poor man's option, you can also save the value to ViewState as another
    value:

    ViewState["ControlCurrentValue"] = ControlName.Text;

    You can then pull this and check. NOTE, however, that this is not wise for
    all controls on a page, esp. a large page, as ViewState will get very large.
    It is less of a problem in 2.0, but still a problem. This is necessary if you
    are pulling the value at Render (see no reason to do this, of course).

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************


    "epigram" wrote:

    > I'm responding to a button click event on an asp.net web form. I then need
    > to retrieve the value from a TextBox control and I want to compare it
    > against the control's previous value to see if it has changed. How can I
    > retrieve a control's previous value from the ViewState? I know that I could
    > save the control's previous value in a session variable, reretrieve it's
    > value from the db, etc. But it would appear the control's previous value
    > would be in the ViewState since that is what ASP.NET uses to fire events
    > such as TextChanged.
    >
    > Thanks!
    >
    >
    >
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Jun 3, 2005
    #2
    1. Advertising

  3. epigram

    epigram Guest

    I was asking the first question, although the answer to the second one is
    key! Thanks. The thing I am trying to do is to get to the actual control
    that is stored in the ViewState (or it's text value), not an object that I
    placed into the ViewState statebag. I tried doing this, in my button click
    event handler, with code such as:

    TextBox tbxSaved = (TextBox)ViewState["myTextBoxId"];

    where myTextBoxId is the value of the id attribute of the TextBox control on
    the form. It comes back as <undefined value>. Any ideas what I am doing
    wrong or if this is possible?

    Thanks.


    "Cowboy (Gregory A. Beamer) - MVP" <> wrote
    in message news:...
    > Are you asking "how do I pull from viewstate?" ViewState is a statebag
    > object, which has key/value pairs. To pull from viewstate, you can pull
    > directly from ViewState:
    >
    > //direct pull
    > string value = (string) ViewState["keyName"];
    >
    > The key here is a) pulling the correct value and b) pulling at the right
    > time (before it is reset). This is rather easy, as the order is (i.e., are
    > you asking "when can I pull from Viewstate?"):
    >
    > Page.Init
    > Page.LoadViewState
    > Page.ProcessPostData
    > Page.Load
    > Change events for controls
    > Server side validation
    > Button.Click or Button.Command events
    > Page.PreRender
    > Page.SaveViewState <<< This is where it is too late to pull the old value
    > Page.Render
    >
    > As long as you grab the old value before Page.SaveViewState, you are fine.
    >
    > As a poor man's option, you can also save the value to ViewState as
    > another
    > value:
    >
    > ViewState["ControlCurrentValue"] = ControlName.Text;
    >
    > You can then pull this and check. NOTE, however, that this is not wise for
    > all controls on a page, esp. a large page, as ViewState will get very
    > large.
    > It is less of a problem in 2.0, but still a problem. This is necessary if
    > you
    > are pulling the value at Render (see no reason to do this, of course).
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > ***************************
    > Think Outside the Box!
    > ***************************
    >
    >
    > "epigram" wrote:
    >
    >> I'm responding to a button click event on an asp.net web form. I then
    >> need
    >> to retrieve the value from a TextBox control and I want to compare it
    >> against the control's previous value to see if it has changed. How can I
    >> retrieve a control's previous value from the ViewState? I know that I
    >> could
    >> save the control's previous value in a session variable, reretrieve it's
    >> value from the db, etc. But it would appear the control's previous value
    >> would be in the ViewState since that is what ASP.NET uses to fire events
    >> such as TextChanged.
    >>
    >> Thanks!
    >>
    >>
    >>
    epigram, Jun 3, 2005
    #3
    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. Softwaremaker
    Replies:
    2
    Views:
    483
    Rajesh.V
    Oct 27, 2003
  2. David J Duryea

    VB Newbie, Retrieve Listbox value from Control

    David J Duryea, Jan 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,471
    Natty Gur
    Jan 27, 2004
  3. epigram
    Replies:
    0
    Views:
    378
    epigram
    Jun 3, 2005
  4. Josema
    Replies:
    2
    Views:
    325
  5. Josema
    Replies:
    0
    Views:
    191
    Josema
    May 9, 2005
Loading...

Share This Page