RE: ViewState: why?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance






"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!
***************************
.



Relevant Pages

  • Re: WebCustomControl and session
    ... you have access to Session, Cache, Application, Request, Response... ... Cause my control makes a call to the .aspx using ... and this aspx is internal part of the control. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Parameters VS Session Values
    ... When you first use a site a Session is created and the Session ID is stored ... With each subsequent page request that cookie is given to the ... Is there a relation between Session Variables and cookies? ... The url can be changed by the user and you can't control it ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: NadaNet for the //c - just an idea
    ... chip/etc before/after each packet to be sent/received? ... control over write protect with the drive still running, ... properly *throughout* a whole request. ... able to write to the net to assert its lock). ...
    (comp.sys.apple2)
  • RE: Security Exception in TslStream.EndRead
    ... Microsoft Online Support ... | WebRequest component to request a SSL protected remote services. ... | machine (or grant the control assembly full trust) and run the page again ... | | Thread-Topic: Security Exception in TslStream.EndRead ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: Server 2008: Shared or concurrent remote desktop access
    ... Full Control with user's permission ... Full Control without user's permission ... View Session without user's permission. ... Is there any way at all with Windows Server 2008's remote desktop (or other ...
    (microsoft.public.windows.server.general)