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. Advertisements

  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, May 11, 2007
    #2
    1. Advertisements

  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?
     
    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
     
    Teemu Keiski, May 17, 2007
    #4
  5. Thanks very much!

    Carlo

     
    Carlo Razzeto, May 18, 2007
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.