ViewState bug? Or am I missing something?

Discussion in 'ASP .Net Building Controls' started by Carlo Razzeto, May 8, 2007.

  1. I have a few user controls which use the view state to store the UI value.
    I'm running into a bit of an odd error. Many of my controls use a hidden
    value in the form to store the data. The reason for this is to seperate the
    internal value from the display, and still allow users to access the control
    data by using Request.Form("ControlName"). In order to populate the .Text
    property, I have a UserControl.Init event handler which basically does the
    following:

    sub Page_Init( ... ) handles me.init
    Text = Request.Form(ClientID)
    end sub

    The set for the text property stores this directly into the view state like
    ViewState(ClientID + ":Text") = value.

    In this control, I also have a PreRender event handler. This event handler
    will populate the display items for the control, as well as set any
    javascript etc.

    Sub Page_PreRender( ... ) Handles me.prerender
    txtDisplay.Text = GetDisplayText()
    txtDisplay.Attributes.Add("onchange", "...")....
    End Sub

    What I've found by stepping through the code is the following...

    When the Text property is set in the Page_Init method, the value is correct
    as per the controls state when the form was posted. For some reason, when it
    gets to the Page_PreRender method, all of the sudden instead of the
    VeiwState containing the updated text value, it has the text value that was
    set when the page was initally loaded. Why is this? It seems like
    ViewState() = ... does not store values between various page events. This
    seems like a rather peculier behaviour to me (which also seems to defeat the
    purpose of ViewState as I understand it). Thanks for any thoughts

    Carlo
     
    Carlo Razzeto, May 8, 2007
    #1
    1. Advertising

  2. Carlo Razzeto

    Teemu Keiski Guest

    Hi,

    basically ViewState isn't tracked at that point of Page lifecycle and when
    you set something to viewstate at that point, it isn't marked to be dirty
    for persisting (event though it exists in the viewstate collection for the
    rwquest)

    Try moving the code to Page_Load and see if that helps.


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "Carlo Razzeto" <> wrote in message
    news:...
    >I have a few user controls which use the view state to store the UI value.
    >I'm running into a bit of an odd error. Many of my controls use a hidden
    >value in the form to store the data. The reason for this is to seperate the
    >internal value from the display, and still allow users to access the
    >control data by using Request.Form("ControlName"). In order to populate the
    >.Text property, I have a UserControl.Init event handler which basically
    >does the following:
    >
    > sub Page_Init( ... ) handles me.init
    > Text = Request.Form(ClientID)
    > end sub
    >
    > The set for the text property stores this directly into the view state
    > like ViewState(ClientID + ":Text") = value.
    >
    > In this control, I also have a PreRender event handler. This event handler
    > will populate the display items for the control, as well as set any
    > javascript etc.
    >
    > Sub Page_PreRender( ... ) Handles me.prerender
    > txtDisplay.Text = GetDisplayText()
    > txtDisplay.Attributes.Add("onchange", "...")....
    > End Sub
    >
    > What I've found by stepping through the code is the following...
    >
    > When the Text property is set in the Page_Init method, the value is
    > correct as per the controls state when the form was posted. For some
    > reason, when it gets to the Page_PreRender method, all of the sudden
    > instead of the VeiwState containing the updated text value, it has the
    > text value that was set when the page was initally loaded. Why is this? It
    > seems like ViewState() = ... does not store values between various page
    > events. This seems like a rather peculier behaviour to me (which also
    > seems to defeat the purpose of ViewState as I understand it). Thanks for
    > any thoughts
    >
    > Carlo
     
    Teemu Keiski, May 11, 2007
    #2
    1. Advertising

  3. No way to mark it dirty? This particular code needs to be in Init as the
    property needs to be set prior to the main page load event being fired.
    Thanks!

    Carlo

    ----- Original Message -----
    From: "Teemu Keiski" <>
    Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    Sent: Friday, May 11, 2007 5:13 AM
    Subject: Re: ViewState bug? Or am I missing something?


    > Hi,
    >
    > basically ViewState isn't tracked at that point of Page lifecycle and when
    > you set something to viewstate at that point, it isn't marked to be dirty
    > for persisting (event though it exists in the viewstate collection for the
    > rwquest)
    >
    > Try moving the code to Page_Load and see if that helps.
    >
    >
    > --
    > Teemu Keiski
    > AspInsider, ASP.NET MVP
    > http://blogs.aspadvice.com/joteke
    > http://teemukeiski.net
    >
    >
    > "Carlo Razzeto" <> wrote in message
    > news:...
    >>I have a few user controls which use the view state to store the UI value.
    >>I'm running into a bit of an odd error. Many of my controls use a hidden
    >>value in the form to store the data. The reason for this is to seperate
    >>the internal value from the display, and still allow users to access the
    >>control data by using Request.Form("ControlName"). In order to populate
    >>the .Text property, I have a UserControl.Init event handler which
    >>basically does the following:
    >>
    >> sub Page_Init( ... ) handles me.init
    >> Text = Request.Form(ClientID)
    >> end sub
    >>
    >> The set for the text property stores this directly into the view state
    >> like ViewState(ClientID + ":Text") = value.
    >>
    >> In this control, I also have a PreRender event handler. This event
    >> handler will populate the display items for the control, as well as set
    >> any javascript etc.
    >>
    >> Sub Page_PreRender( ... ) Handles me.prerender
    >> txtDisplay.Text = GetDisplayText()
    >> txtDisplay.Attributes.Add("onchange", "...")....
    >> End Sub
    >>
    >> What I've found by stepping through the code is the following...
    >>
    >> When the Text property is set in the Page_Init method, the value is
    >> correct as per the controls state when the form was posted. For some
    >> reason, when it gets to the Page_PreRender method, all of the sudden
    >> instead of the VeiwState containing the updated text value, it has the
    >> text value that was set when the page was initally loaded. Why is this?
    >> It seems like ViewState() = ... does not store values between various
    >> page events. This seems like a rather peculier behaviour to me (which
    >> also seems to defeat the purpose of ViewState as I understand it). Thanks
    >> for any thoughts
    >>
    >> Carlo

    >
     
    Carlo Razzeto, May 17, 2007
    #3
  4. Carlo Razzeto

    Teemu Keiski Guest

    Hi,

    Setting it after TrackViewState has occurred marks added item dirty
    automatically. At page_load it is, that's why I recommended using it.

    If you need it to be set just before Page_Load event, override OnLoad method
    of the page where you set it before calling MyBase.OnLoad (because PAge_load
    event occurs when you call base method). If you use ASP.NEt 2.0, you could
    just handle PreLoad event of the Page.

    See: http://www.eggheadcafe.com/articles/20051227.asp


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net





    "Carlo Razzeto" <> wrote in message
    news:...
    > No way to mark it dirty? This particular code needs to be in Init as the
    > property needs to be set prior to the main page load event being fired.
    > Thanks!
    >
    > Carlo
    >
    > ----- Original Message -----
    > From: "Teemu Keiski" <>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    > Sent: Friday, May 11, 2007 5:13 AM
    > Subject: Re: ViewState bug? Or am I missing something?
    >
    >
    >> Hi,
    >>
    >> basically ViewState isn't tracked at that point of Page lifecycle and
    >> when you set something to viewstate at that point, it isn't marked to be
    >> dirty for persisting (event though it exists in the viewstate collection
    >> for the rwquest)
    >>
    >> Try moving the code to Page_Load and see if that helps.
    >>
    >>
    >> --
    >> Teemu Keiski
    >> AspInsider, ASP.NET MVP
    >> http://blogs.aspadvice.com/joteke
    >> http://teemukeiski.net
    >>
    >>
    >> "Carlo Razzeto" <> wrote in message
    >> news:...
    >>>I have a few user controls which use the view state to store the UI
    >>>value. I'm running into a bit of an odd error. Many of my controls use a
    >>>hidden value in the form to store the data. The reason for this is to
    >>>seperate the internal value from the display, and still allow users to
    >>>access the control data by using Request.Form("ControlName"). In order to
    >>>populate the .Text property, I have a UserControl.Init event handler
    >>>which basically does the following:
    >>>
    >>> sub Page_Init( ... ) handles me.init
    >>> Text = Request.Form(ClientID)
    >>> end sub
    >>>
    >>> The set for the text property stores this directly into the view state
    >>> like ViewState(ClientID + ":Text") = value.
    >>>
    >>> In this control, I also have a PreRender event handler. This event
    >>> handler will populate the display items for the control, as well as set
    >>> any javascript etc.
    >>>
    >>> Sub Page_PreRender( ... ) Handles me.prerender
    >>> txtDisplay.Text = GetDisplayText()
    >>> txtDisplay.Attributes.Add("onchange", "...")....
    >>> End Sub
    >>>
    >>> What I've found by stepping through the code is the following...
    >>>
    >>> When the Text property is set in the Page_Init method, the value is
    >>> correct as per the controls state when the form was posted. For some
    >>> reason, when it gets to the Page_PreRender method, all of the sudden
    >>> instead of the VeiwState containing the updated text value, it has the
    >>> text value that was set when the page was initally loaded. Why is this?
    >>> It seems like ViewState() = ... does not store values between various
    >>> page events. This seems like a rather peculier behaviour to me (which
    >>> also seems to defeat the purpose of ViewState as I understand it).
    >>> Thanks for any thoughts
    >>>
    >>> Carlo

    >>

    >
     
    Teemu Keiski, May 17, 2007
    #4
  5. Thanks very much!

    Carlo

    "Teemu Keiski" <> wrote in message
    news:...
    > Hi,
    >
    > Setting it after TrackViewState has occurred marks added item dirty
    > automatically. At page_load it is, that's why I recommended using it.
    >
    > If you need it to be set just before Page_Load event, override OnLoad
    > method of the page where you set it before calling MyBase.OnLoad (because
    > PAge_load event occurs when you call base method). If you use ASP.NEt 2.0,
    > you could just handle PreLoad event of the Page.
    >
    > See: http://www.eggheadcafe.com/articles/20051227.asp
    >
    >
    > --
    > Teemu Keiski
    > AspInsider, ASP.NET MVP
    > http://blogs.aspadvice.com/joteke
    > http://teemukeiski.net
    >
    >
    >
    >
    >
    > "Carlo Razzeto" <> wrote in message
    > news:...
    >> No way to mark it dirty? This particular code needs to be in Init as the
    >> property needs to be set prior to the main page load event being fired.
    >> Thanks!
    >>
    >> Carlo
    >>
    >> ----- Original Message -----
    >> From: "Teemu Keiski" <>
    >> Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    >> Sent: Friday, May 11, 2007 5:13 AM
    >> Subject: Re: ViewState bug? Or am I missing something?
    >>
    >>
    >>> Hi,
    >>>
    >>> basically ViewState isn't tracked at that point of Page lifecycle and
    >>> when you set something to viewstate at that point, it isn't marked to be
    >>> dirty for persisting (event though it exists in the viewstate collection
    >>> for the rwquest)
    >>>
    >>> Try moving the code to Page_Load and see if that helps.
    >>>
    >>>
    >>> --
    >>> Teemu Keiski
    >>> AspInsider, ASP.NET MVP
    >>> http://blogs.aspadvice.com/joteke
    >>> http://teemukeiski.net
    >>>
    >>>
    >>> "Carlo Razzeto" <> wrote in message
    >>> news:...
    >>>>I have a few user controls which use the view state to store the UI
    >>>>value. I'm running into a bit of an odd error. Many of my controls use a
    >>>>hidden value in the form to store the data. The reason for this is to
    >>>>seperate the internal value from the display, and still allow users to
    >>>>access the control data by using Request.Form("ControlName"). In order
    >>>>to populate the .Text property, I have a UserControl.Init event handler
    >>>>which basically does the following:
    >>>>
    >>>> sub Page_Init( ... ) handles me.init
    >>>> Text = Request.Form(ClientID)
    >>>> end sub
    >>>>
    >>>> The set for the text property stores this directly into the view state
    >>>> like ViewState(ClientID + ":Text") = value.
    >>>>
    >>>> In this control, I also have a PreRender event handler. This event
    >>>> handler will populate the display items for the control, as well as set
    >>>> any javascript etc.
    >>>>
    >>>> Sub Page_PreRender( ... ) Handles me.prerender
    >>>> txtDisplay.Text = GetDisplayText()
    >>>> txtDisplay.Attributes.Add("onchange", "...")....
    >>>> End Sub
    >>>>
    >>>> What I've found by stepping through the code is the following...
    >>>>
    >>>> When the Text property is set in the Page_Init method, the value is
    >>>> correct as per the controls state when the form was posted. For some
    >>>> reason, when it gets to the Page_PreRender method, all of the sudden
    >>>> instead of the VeiwState containing the updated text value, it has the
    >>>> text value that was set when the page was initally loaded. Why is this?
    >>>> It seems like ViewState() = ... does not store values between various
    >>>> page events. This seems like a rather peculier behaviour to me (which
    >>>> also seems to defeat the purpose of ViewState as I understand it).
    >>>> Thanks for any thoughts
    >>>>
    >>>> Carlo
    >>>

    >>

    >
     
    Carlo Razzeto, May 18, 2007
    #5
    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. Guest
    Replies:
    4
    Views:
    483
    Guest
    Oct 13, 2004
  2. Chris Metzler
    Replies:
    15
    Views:
    555
    Tim Peters
    Sep 27, 2004
  3. Dustin J. Mitchell
    Replies:
    0
    Views:
    286
    Dustin J. Mitchell
    Oct 19, 2006
  4. Pekka Järvinen
    Replies:
    2
    Views:
    668
    Richard Tobin
    Apr 29, 2008
  5. Patrick Li
    Replies:
    9
    Views:
    121
    Patrick Li
    Aug 28, 2008
Loading...

Share This Page