Re: ViewState: why?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



VieState is used to store per user information among each requests and
postback.
If you don't store the value in the viewstate, the property value will be
lost (since each request instanciate a new webform object), or you will have
to set it up again through your code or aspx pages html content.

There's other "store" you can use (in .NEt 1.1)
- Application : All users store the same Application variables. But
beware... if you use web farm, each serveur will have its own application
collection
- Session : Each user has its own. If you use web farm, you can configure
one of the servers to act as a state server, or you can use SQL Server. If
you don't, requests may hit another server in the farm and you'll loose the
session.
- Context : each request hold a context collection that is lost at the end
of the request. Usefull if you want to share informations accross all your
class and often filed in global.asax

My opinion is that the viewstate should be use as soon as you have a value
in a control that must persist. Storing it on the client side has the
advantage of less memory used on the server (Imagine 100's of variable in
the session * number of users --- it can grow very quickly).
Ok, it's take some KB on the page, but the most weigth in a page comes from
other resources like pictures.

Finally, do not forget that a developper that use a control can use the
EnableViewState to disable the ability to store data on the client side.

Hope that help.

Steve

"Lloyd Dupont" <net.galador@ld> a écrit dans le message de news:
%23VuThc7CGHA.2596@xxxxxxxxxxxxxxxxxxxxxxx
> 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?
> 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.
>
> 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.
>
> Such as: I use ViewState to store property (in case they need to be
> stored), yet I doesn't output this viewstate to the web page, in case it
> doesn't need to be restored (would be equal to value in the aspx)?
> Yet that seems quite a marginal
>


.



Relevant Pages

  • Re: Properties instead of Session Variables
    ... Use cookies, viewstate, file, or DB ... Keep in mind that http is stateless that is server side stuff is ... with each new request to recreate the page in its previous state). ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ViewState: why?
    ... the property, the user clicks a button, and there is another request to the ... and each request to the server means a new ... The ViewState approach works, because the viewstate is posted back to the ... > public string Text ...
    (microsoft.public.dotnet.framework.aspnet)
  • Viewstate - load balance web site - Losing state
    ... our viewstate gets "lost" between requests. ... Everytime the request move from one server to the other the ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Static Variable vs View State
    ... store it on the server with the SessionPageStatePersister. ... I am facing problem with the huge Viewstate size. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Exchange Mailbox Size
    ... This seems like a foolish request ... servers (Broadly 40GB per store and 20 stores per server) ... mailbox. ... How many mailboxes are that large on that server, ...
    (microsoft.public.exchange.admin)