ViewState in ASP.NET

Last post 04-20-2008, 3:18 PM by ashishvj. 2 replies.
Sort Posts: Previous Next
  •  10-10-2007, 3:06 PM Post number 38079

    ViewState in ASP.NET

    Hello Friends,

    Can somebody please explain me how to make proper use of ViewState for better website performance? 

    Waiting for positive reply.

     


    Give people what they expect and do it cheerfully.
  •  10-10-2007, 3:17 PM Post number 38080 in reply to post number 38079

    • Damon is not online. Last active: 11-26-2008, 11:06 AM Damon
    • Top 10 Contributor
    • Joined on 06-26-2006
    • Dallas, TX
    • Acorn Archimedes

    Re: ViewState in ASP.NET

    The viewstate stores a serialized copy of state information for your controls.  For example, if you databind a grid, the grid can store all of the data to which it is bound in the ViewState so you don't have to re-populate the grid on the next post back (in theory, this alleviates having to re-hit the database for the data).

    This can be both a blessing a curse because if your grid is huge, then your ViewState is likely also very large.  And the problem with the view state is that it goes down to the browser, then from the browser back to the server, so it's a double-hit.  If you are on an internal network, this may not be a big deal because that information goes really fast.  If you are on a dial-up connection then it's going to suck big time.  A lot of people are on broadband connections, and they sit somwhere in the middle.  The page may be a bit sluggish, but it's not ridiculous.

    I tend to use the viewstate only for small pieces of data.  For data displayed in grids, I turn viewstate off, cache the data, and rebind on every request.  This alleviates multiple database hits and keeps the viewstate to a minimum.  If caching isn't an option, then I just bit the bullet and hit the database again but attempt to keep the infomation comming back to a minimum (e.g. paging the records).

    You do have to be careful, however, because some controls will not fire their events if they are not bound when the control initializes.  When the viewstate is on, Events will fire correctly because the serialized information is populated into the control.  If you turn the viewstate off, you have to manually bind in the initialization so the events fire correctly.


    Damon Armstrong, Technology Consultant
    [Blog] [Articles]
  •  04-20-2008, 3:18 PM Post number 48022 in reply to post number 38080

    Re: ViewState in ASP.NET

    hi thr.. actually am having problem with my gridview. i am having all the data from excel sheet into my gridview. but i dun want extra blanck rows from excel sheet into my gridview. wats the logic n if u mail me sample code then its wel n gud.. i m tired of this problem.

    plz help me

    thank you

View as RSS news feed in XML