Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Handling ViewState and properties

Reply
Thread Tools

Handling ViewState and properties

 
 
Guest
Posts: n/a
 
      03-09-2005
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 ?



 
Reply With Quote
 
 
 
 
quaester
Guest
Posts: n/a
 
      03-09-2005
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 ?
>
>
>
>

 
Reply With Quote
 
 
 
 
marc.derider@gmail.com
Guest
Posts: n/a
 
      03-09-2005
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

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      03-10-2005
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.)


 
Reply With Quote
 
marc.derider@gmail.com
Guest
Posts: n/a
 
      03-10-2005
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.

 
Reply With Quote
 
marc.derider@gmail.com
Guest
Posts: n/a
 
      03-10-2005
Hmmm it seems responses are not getting posted properly.

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      03-11-2005
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.)




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with ViewState: "function 'ViewState.get_Item' evaluated and returned null" Steph ASP .Net 2 05-11-2011 02:35 PM
Custom Controls, Public properties and the Viewstate trullock@googlemail.com ASP .Net 1 02-19-2008 06:28 PM
ViewState properties and mapped properties don't work well togethe Christophe Peillet ASP .Net Building Controls 2 01-21-2006 07:35 PM
CompositeControls: ViewState properties w/ Mapped properties probl =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?= ASP .Net 1 01-19-2006 09:19 AM
ViewState properties and mapped properties don't work well togethe Christophe Peillet ASP .Net Web Controls 1 01-19-2006 09:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57