Q - How do I persist members variables/properties on Page level?

U

Usenet User

I know I can use ViewState for member fields and variables declared on
Page level to persist between postbacks, for example:

private const string FormModeTag = "_FormMode_";

private string FormMode
{
get
{
object formMode = this.ViewState[FormModeTag];
if( formMode != null )
{
return (string)formMode;
}
else
{
return "";
}
}
set
{
this.ViewState[FormModeTag] = value;
}
}


Is there some other and/or better way? I found PersistentMode
attribute, but it seems it only works with controls' properties, not
with Page properties.

Thanks!
 
S

Steve C. Orr [MVP, MCSD]

ViewState is considered the best practice (in almost all cases) for
persisting a value between postbacks on the same page. It has the perfect
scope for such an operation. There are of course other caching options but
they have a less than ideal scope.
 
B

Brock Allen

Is there some other and/or better way?

What do you mean? This is the model for insuring a value is passed back and
forth between postbacks.
I found PersistentMode
attribute, but it seems it only works with controls' properties, not
with Page properties.

PersistMode has to do with how the syntax in the ASPX is interpreted by the
ASP.NET parser. It has nothing to do with ViewState.
 
K

Kevin Spencer

Is there some other and/or better way?

ViewState is a tool. Asking if there's a better way is like asking if there
is a better tool than a hammer. It all depends on what you are doing. A
hammer is perfect for hammering nails, but lousy at cutting plywood.

The most important thing is to know what all of your tools are, what each
one is used for, what its characteristics are, and when it is appropriate to
use it.

ViewState, for example, is not particularly good for storing large chunks of
data. It slows down the Response time by adding to the content of the Page.
It is, however, perfect for storing the state of a Server Control, which is
very small. It has the advantage of not consuming resources (memory) on the
server side.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
U

Usenet User

ViewState is a tool. Asking if there's a better way is like asking if there
is a better tool than a hammer. It all depends on what you are doing. A
hammer is perfect for hammering nails, but lousy at cutting plywood.

The most important thing is to know what all of your tools are, what each
one is used for, what its characteristics are, and when it is appropriate to
use it.

ViewState, for example, is not particularly good for storing large chunks of
data. It slows down the Response time by adding to the content of the Page.
It is, however, perfect for storing the state of a Server Control, which is
very small. It has the advantage of not consuming resources (memory) on the
server side.

Thank you all who responded, I am getting the idea. I only see a
couple of drawbacks with ViewState:

1) Must be implemented manually, per each variable/property.

2) Uncovers the inner data of the class by sending it to the client.
What if it is not meant to be seen?
 
S

Steve C. Orr [MVP, MCSD]

ViewState is encoded so its content cannot be easily seen by a typical user.
Also, ViewState can be configured to be stored on the server instead of the
client, if you prefer.
 

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,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top