I have come across this line in a program I have to ammend :
What exactly is Viewstate ?Code:Viewstate("lDDLDataTable") = fDDL2.DataSource
Is it similar to Session state ?
Printable View
I have come across this line in a program I have to ammend :
What exactly is Viewstate ?Code:Viewstate("lDDLDataTable") = fDDL2.DataSource
Is it similar to Session state ?
Yes, except that the Session is held on the server, while the ViewState is sent back and forth between the client and server with each transmission. Check the source of an aspx page and you will see a hidden field called "__VIEWSTATE". It is an encoding of all the data held in the ViewState object. Obviously, the more you add to the ViewState, the bigger this hidden field will get. And the more you will be transmitting across the wire.
So are you saying that when I add something to viewstate, lets say called "myvalue" then it is a bit like creating a hidden field in classic asp called "myvalue" and assigning a value to it ?
You can see the contents of the viewstate by decoding it from Base64...
Throw two multiline textboxes in a windows form app, and throw the below code in the button click event.
When you are viewing the source of the rendered page in the web browser, copy the value tag of the viewstate, and paste it into the first textbox, then click the button, and you can see how your viewstate is rendered (and what it is storing).
VB Code:
Dim binaryData() As Byte binaryData = System.Convert.FromBase64String(TextBox1.Text.Trim) Dim utf As New System.Text.UTF7Encoding Dim s As String = utf.GetString(binaryData) TextBox2.Text = s
Personally I try to remove the runat=server part of server tags unless I specifically need to add stuff to the viewstate. Sometimes it just gets so big it takes too long to download for modem users...
Well yes, I used to make the mistake of using ASP:Labels for static text, which was stupid, not because it was being stored in viewstate, but because it incurs (hardly noticeable on a small page) more server processing.
There's also the Page Directive switch to change SessionState to ReadOnly for pages that only ever read from SessionState, or Disabled for pages that never need to access SessionState.
It's also worth mentioning that not all objects are able to be persisted to viewstate. The class must be serializable, either by applying the Serializable attribute to the class or implementing the ISerializable interface.
Now I am confused.
Thanks, my head is like jelly.
Viewstate basically allows for the persistence of values in between page postbacks. If you were to be creating your own control, then it'd be something you'd definitely use. Here's an example I was playing with. I created a label-type control which would display text in bold, underline, italics, etc, depending upon the value you pass to it.
Now, if a postback were to occur, I'd need to preserve the fact that Bold is True.
VB Code:
Public Property Bold() As Boolean Get Return CType(ViewState("Bold"), Boolean) End Get Set(ByVal Value As Boolean) ViewState("Bold") = Value End Set End Property
See what I mean?
Of course, if you set the EnableViewState property of the control to False, then ViewState just won't work, and you'll lose the values in between postbacks.
Clever frog. :wave:
I like jelly.:)
I use postbacks on my search pages to do custom paging between pages. Viewstate was huge when I was using asp.net's own paging - so I use a form at the top of the page with all hidden fields to repost the request back to my page :)
?? But ASP.NET only allows one form at a time....???
I use it in the HTML section of the page and remove ASP.NET's own form tag :)
AH.... slick.
I think it's worth bringing up that if you create your own Page class you can do what I show below and your override viewstate is never downloaded it's stored on the server.
It saves some b-width......PHP Code:/// <summary>
/// Save ViewState.
/// </summary>
/// <param name="viewState">The ViewState.</param>
protected override void SavePageStateToPersistenceMedium(object viewState)
{
if(_useSessionViewState)
{
this.Session[this.Request.Path + "-vstate"] = viewState;
}
else
{
base.SavePageStateToPersistenceMedium(viewState);
}
}
/// <summary>
/// Load ViewState.
/// </summary>
/// <returns>The ViewState.</returns>
protected override object LoadPageStateFromPersistenceMedium()
{
if(_useSessionViewState)
{
if(this.Session[this.Request.Path + "-vstate"] == null)
{
this.Response.Redirect(this.Request.ApplicationPath);
}
return this.Session[this.Request.Path + "-vstate"];
}
else
{
return base.LoadPageStateFromPersistenceMedium();
}
}
You can also serialize view stae save it to the server and reload it if for instance the session timed out and a user was in the middle of a large form or something......
True, and then you're eating up server memory. So its tradeoffs you have to consider.
Taking it out of memory and Loading viewstate from a file or dbase also slows down performance....
Forgive me for saying this gents but what exactly is custom paging ?
I always thought paging was for datagrids e.t.c.
Yup :)
It's when you inherit from that Web.UI.Page this and make your own special Web.UI.Page thing.
I have one I use to catch .fish... ><> but that really hard to explain......
Really though you can make your own class and inherit for the page class and add functionality to page then you set your web forms to inherit from your page class.
I have one that loads some DHTML API sets the stus message kick you out if you aren't logged in keeps viewstate on the session. OH btw I'm working on a way to ensure it clears(the session viewstate) and doesn't sit in the session for hrs because they are on the site.
I am really confused now.