Results 1 to 22 of 22

Thread: What exactly is Viewstate ?

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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 ?

    Parksie

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    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 ?

    Parksie

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Dim binaryData() As Byte
    2.         binaryData = System.Convert.FromBase64String(TextBox1.Text.Trim)
    3.         Dim utf As New System.Text.UTF7Encoding
    4.         Dim s As String = utf.GetString(binaryData)
    5.  
    6.         TextBox2.Text = s

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  8. #8

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Now I am confused.

    Thanks, my head is like jelly.

    Parksie

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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:
    1. Public Property Bold() As Boolean
    2.             Get
    3.                 Return CType(ViewState("Bold"), Boolean)
    4.             End Get
    5.             Set(ByVal Value As Boolean)
    6.                 ViewState("Bold") = Value
    7.             End Set
    8.         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.

  10. #10

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Clever frog.

    Parksie

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I like jelly.

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  13. #13
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    ?? But ASP.NET only allows one form at a time....???

  14. #14
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  15. #15
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    AH.... slick.

  16. #16
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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.

  17. #17
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Arrow 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.

  18. #18
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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....

  19. #19

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Forgive me for saying this gents but what exactly is custom paging ?

    I always thought paging was for datagrids e.t.c.

    Parksie

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yup
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  21. #21
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  22. #22

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I am really confused now.

    Parksie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width