proper way to initialize/use persistent properties

S

SilentCry

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top