|
-
Aug 10th, 2004, 03:55 AM
#1
Thread Starter
Fanatic Member
What exactly is Viewstate ?
I have come across this line in a program I have to ammend :
Code:
Viewstate("lDDLDataTable") = fDDL2.DataSource
What exactly is Viewstate ?
Is it similar to Session state ?
-
Aug 10th, 2004, 05:49 AM
#2
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.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Aug 10th, 2004, 06:40 AM
#3
Thread Starter
Fanatic Member
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 ?
-
Aug 10th, 2004, 06:46 AM
#4
I wonder how many charact
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
-
Aug 10th, 2004, 06:58 AM
#5
Retired VBF Adm1nistrator
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...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 10th, 2004, 07:17 AM
#6
I wonder how many charact
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.
-
Aug 10th, 2004, 07:42 AM
#7
PowerPoster
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.
-
Aug 10th, 2004, 08:03 AM
#8
Thread Starter
Fanatic Member
Now I am confused.
Thanks, my head is like jelly.
-
Aug 11th, 2004, 01:53 AM
#9
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.
-
Aug 11th, 2004, 05:52 AM
#10
Thread Starter
Fanatic Member
Clever frog.
-
Aug 11th, 2004, 05:57 AM
#11
I like jelly.
-
Aug 11th, 2004, 08:06 AM
#12
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 12th, 2004, 02:44 PM
#13
I wonder how many charact
?? But ASP.NET only allows one form at a time....???
-
Aug 13th, 2004, 02:52 AM
#14
Retired VBF Adm1nistrator
I use it in the HTML section of the page and remove ASP.NET's own form tag
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 13th, 2004, 06:58 AM
#15
I wonder how many charact
-
Aug 19th, 2004, 11:26 AM
#16
Frenzied Member
Session ViewState
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.
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();
}
}
It saves some b-width......
Magiaus
If I helped give me some points.
-
Aug 19th, 2004, 11:28 AM
#17
Frenzied Member
also
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......
Magiaus
If I helped give me some points.
-
Aug 19th, 2004, 03:16 PM
#18
I wonder how many charact
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....
-
Aug 20th, 2004, 04:07 AM
#19
Thread Starter
Fanatic Member
Forgive me for saying this gents but what exactly is custom paging ?
I always thought paging was for datagrids e.t.c.
-
Aug 20th, 2004, 04:31 AM
#20
Retired VBF Adm1nistrator
Yup
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Aug 20th, 2004, 10:16 AM
#21
Frenzied Member
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.
Magiaus
If I helped give me some points.
-
Aug 20th, 2004, 10:40 AM
#22
Thread Starter
Fanatic Member
I am really confused now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|