Handling ViewState and properties

Discussion in 'ASP .Net Web Controls' started by Guest, Mar 9, 2005.

  1. Guest

    Guest Guest

    I was wondering if there was already a common practice / pattern before I
    run off and try building a better wheel. How do developers handle Loading
    the Viewstate when the user has set a value prior to the ViewStateBeing
    loaded.
    For example upon a prior Postback a control is disabled or set to another
    state.
    This state was stored in the viewstate.
    Now this time the user has chosen to do some logic in the OnInit of the Page
    and he sees that his conditions are matched so he sets the Control who was
    prior disabled to enabled. Do most developers maintian a table so that this
    property is not overwritten when the ViewState is loaded or do most
    developers just let it overwrite ?
     
    Guest, Mar 9, 2005
    #1
    1. Advertising

  2. Guest

    quaester Guest

    not sure of any best practices, but my suggestion is to write to the
    viewstate using your code, so this will be consistent with the viewstate
    processing model.

    "" wrote:

    > I was wondering if there was already a common practice / pattern before I
    > run off and try building a better wheel. How do developers handle Loading
    > the Viewstate when the user has set a value prior to the ViewStateBeing
    > loaded.
    > For example upon a prior Postback a control is disabled or set to another
    > state.
    > This state was stored in the viewstate.
    > Now this time the user has chosen to do some logic in the OnInit of the Page
    > and he sees that his conditions are matched so he sets the Control who was
    > prior disabled to enabled. Do most developers maintian a table so that this
    > property is not overwritten when the ViewState is loaded or do most
    > developers just let it overwrite ?
    >
    >
    >
    >
     
    quaester, Mar 9, 2005
    #2
    1. Advertising

  3. Guest

    Guest

    I appreciate the completely unrelated comment. I will go ahead and
    restate my question again.

    Is there an existing design / preference to handling how properties
    that are stored in a viewstate are handled when a developer chooses to
    set the value of one of these properties before the view state is
    loaded.

    Do most control developers consider that person to be sol and overwrite
    changes he made prior to viewstate being loaded.
    Do most control developers allow this change to be made and if so is
    there a tried and proven method to keeping track of tehse changes so
    that a viewstate restoral does not overwrite properties that have been
    set prior to the viewstate being loaded while restoring values that
    have not been set
     
    , Mar 9, 2005
    #3
  4. Hi Marc,

    From your description, you're wondering some best means to manage the
    WebControl's custom properteis which will be persisted in ViewState.
    Also, sometimes when we adjust those properties before the Page's
    LoadViewState, they should be prevented from being override by the old data
    retrieved in LoadViewState, yes?

    I'm not sure whether there is any well-known pattern on this. As my own
    opinion, I'll define my Control properties as the following style. The
    public property is a wrapper of a private member variable which hold the
    actual values and it is not directly accessing the ViewState, we read it
    from and write into ViewState in our override LoadViewState and
    SaveViewState. In addition, we can also define a certain value as the
    default(empty) value of that property, thus, in the LoadViewstate, we can
    determine whether to override the value or not in LoadViewState according
    to whether the private variable's value is the empty value. How do you
    think of this?


    ======================
    ......
    private string title = null;
    ..............

    public string Title
    {
    get
    {
    return title;
    }

    set
    {
    title = value;
    }
    }

    protected override void LoadViewState(object savedState)
    {
    Triplet triplet = (Triplet)savedState;

    if(title == null)
    {
    title = triplet.Second as string;
    }

    base.LoadViewState (triplet.First);
    }

    protected override object SaveViewState()
    {
    object baseState = base.SaveViewState ();

    Triplet triplet = new Triplet();

    triplet.First = baseState;
    triplet.Second = title;

    return triplet;

    }
    ================================

    If you have any other ideas, please feel free to post here.

    Thanks & Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Mar 10, 2005
    #4
  5. Guest

    Guest

    That works good for strings, however for any value-type (Numerical
    values, enumerations, boolean, etc) that cannot be null this idea does
    not operate as well.
     
    , Mar 10, 2005
    #5
  6. Guest

    Guest

    Hmmm it seems responses are not getting posted properly.
     
    , Mar 10, 2005
    #6
  7. Thanks for your followup Marc,

    Yes, I admit that that means may not works very well for value types or
    some other primitive types. But we can still workaround it by defining some
    wellknown const values for empty value. Otherwise, we may need to use
    addiontal variables to indicate whether our properteis should be override
    by VIEWSTATE or not. How do you think?

    Thanks & Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Mar 11, 2005
    #7
    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. Steph
    Replies:
    2
    Views:
    1,688
    heislord5
    May 11, 2011
  2. =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=

    CompositeControls: ViewState properties w/ Mapped properties probl

    =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,159
    Steven Cheng[MSFT]
    Jan 19, 2006
  3. Replies:
    1
    Views:
    294
    bruce barker
    Feb 19, 2008
  4. Christophe Peillet

    ViewState properties and mapped properties don't work well togethe

    Christophe Peillet, Jan 18, 2006, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    177
  5. Christophe Peillet

    ViewState properties and mapped properties don't work well togethe

    Christophe Peillet, Jan 18, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    222
    Steven Cheng[MSFT]
    Jan 19, 2006
Loading...

Share This Page