proper way to initialize/use persistent properties

Discussion in 'ASP .Net Web Controls' started by SilentCry, May 20, 2009.

  1. SilentCry

    SilentCry Guest

    i'm referring to those properties that must implement the ViewState
    collection in order to retain it's value between postbacks. in this case,
    the property is not displayed in the properties box.
    i have a composite control that displays xml in 2 different views - plain
    text and formatted. i have a property called CurViewState that keeps track
    of the current view. it's working as i have it now but i don't know why it
    works given the way i'm initializing it (to text)..

    protected override void OnInit(EventArgs e)
    {
    CurViewState = VIEWSTATE.TEXT;
    }

    [DefaultValue(VIEWSTATE.TEXT)] // <<<------- this seems to do
    nothing when OnInit is commented
    public virtual VIEWSTATE CurViewState
    {
    get
    {
    VIEWSTATE curViewState =
    (VIEWSTATE)ViewState["CurViewState"];
    return curViewState;
    }

    set
    {
    ViewState["CurViewState"] = value;
    }
    }
    (in OnBubbleEvent)
    CommandEventArgs ce = args as CommandEventArgs;
    if (ce != null && ce.CommandName == "SwapView")
    {
    CurViewState = (CurViewState == VIEWSTATE.TEXT) ?
    VIEWSTATE.TREE : VIEWSTATE.TEXT;
    handled = true;
    }


    i find that if i don't initialize it in OnInit, i get an exception in the
    get accessor saying 'object reference not set to instance of object' when i
    run the containing app. this refers to the ViewState var which when init'd
    in OnInit, gets rid of this error. that's fine but since OnInit is executed
    on every postback, how is it the views are still swapped correctly (ie: the
    old value is still retained) when i keep reseting it's value back to Text in
    OnInit? you would think it would always start out as Text and only allow a
    swap to Tree just once but not back again. also, the default value seems to
    do nothing. but yet it works (goes back and forth). this made me think that
    maybe i'm not doing this correctly or i'm just not understanding the timing
    of what's going on. any help would be appreciated.
    SilentCry, May 20, 2009
    #1
    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. -
    Replies:
    1
    Views:
    5,848
    Esmond Pitt
    Jul 26, 2005
  2. Replies:
    0
    Views:
    342
  3. gk
    Replies:
    7
    Views:
    945
    Tom Anderson
    Oct 12, 2010
  4. Sean Ross
    Replies:
    3
    Views:
    106
    Aredridel
    Dec 25, 2003
  5. H. Wade Minter
    Replies:
    8
    Views:
    264
    Robin
    Apr 25, 2004
Loading...

Share This Page