RE: ViewState: why?
- From: "Cowboy (Gregory A. Beamer) - MVP" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 28 Dec 2005 09:00:04 -0800
"Lloyd Dupont" wrote:
> When you define UserControl in source code the sample I see are often like
> that:
> ============
> public string Text
> {
> get
> {
> String s = (String)ViewState["Text"];
> return ((s == null) ? String.Empty : s);
> }
> set
> {
> ViewState["Text"] = value;
> }
> }
> ============
> Yet I found that this simpler approach work well
> ============
> string text;
> public string Text
> {
> get { return text; }
> set { text = value; }
> }
> ============
> It also doesn't clutter the web page (as the view state is embeded in the
> page as an input of HIDDEN type).
>
> So now I wonder, why use the ViewState exactly?
Lifetime control. Essentially, the User Control properties can fall out of
scope after a request. While setting Text, as a property solves the problem
for this request, it does not have lifetime beyond the request. ViewState
does.
You have other options. Session is an option, although I am not too fond of
just throwing things into session as they tend to get "pack ratted" in there
and not cleaned out. You can also use caching mechanisms, including custom
static (Shared for VBers) classes that use session id to store information.
> I guess there are reason to do so, like if I want to programatically change
> the control property with an event handler and keep on further post back.
> Yet that looks to me as a very marginal exemple.
It is marginal in some cases, but being able to hold state on a user control
is important when you do postsbacks.
> Are there better reason?
> And how to mark a control in such a way that it won't store its view state
> in the web page.
In the @ directive you can turn off ViewState.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
.
- Follow-Ups:
- Re: ViewState: why?
- From: Lloyd Dupont
- Re: ViewState: why?
- From: Lloyd Dupont
- Re: ViewState: why?
- References:
- ViewState: why?
- From: Lloyd Dupont
- ViewState: why?
- Prev by Date: Re: temporary file
- Next by Date: Re: ASP.NET 2.0 ICallbackEventHandler
- Previous by thread: Re: ViewState: why?
- Next by thread: Re: ViewState: why?
- Index(es):
Relevant Pages
|