SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Good practices concerning View State

    While going through a recent app and disabling view state in the <asp:content> and <asp:panel> controls I was wondering if viewstate is ever necessary on these controls and are there any other controls that don't really require a view state or would always benefit from having it disabled most of the time?

    Also, is there a way to apply enableviewstate="false" to all content and panel controls site wide without going through each instance and adding it?
    Last edited by imaginekitty; Mar 11, 2009 at 10:43.

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,630
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Disabling viewstate when not needed is always a good idea. For doing things like this on specific classes of controls site-wide, check out control adapters.

  3. #3
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah yes, control adapters. Thanks!

    There are certain items like <asp:content> and <asp:button> that should have viewstate off by default. Maybe.

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,630
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Really depends on what you are doing with the controls. If the page is just staticly displaying information, you are almost certainly in good shape. But, if you are using web form functionality, you could break something easily. Also remember that the disabled viewstate cascades down. So, if you are doing something in your ContentControl that needs viewstate, you might break it by disabling viewstate there.

  5. #5
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    Really depends on what you are doing with the controls. If the page is just staticly displaying information, you are almost certainly in good shape. But, if you are using web form functionality, you could break something easily. Also remember that the disabled viewstate cascades down. So, if you are doing something in your ContentControl that needs viewstate, you might break it by disabling viewstate there.
    Hmm. It doesn't seem to cascade down from a content control. I have a panel control inside of a content control. The content control is set to enableviewstate="false". If I set the panel control to enableviewstate="false" it loses its listview's content on postback. Removing the enableviewstate="false" form the panel but keeping the enableviewstate="false" on the content control maintains the listview's content.

    Am I looking at this the wrong way? Is the content control downstream from the items it contains?

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,630
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Honestly never tried it on the content controls, but it should flow down from panels and/or pages and/or user controls.

  7. #7
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Viewstate is a hidden input on the page. So u cannot enable viewstate on one control and disable it on another. I always disable view state unless i need it. If its only needed on a control you could also look at using ControlState instead

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,630
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You can definitely disable viewstate on individual controls. You can also disable it page-wide. If so, no viewstate will work on that page with all the intended and unintended consequences.

  9. #9
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    You can turn off ViewState on a per-control, per-page, or even per-application basis. You don't need ViewState if:

    Pages:
    * The page doesn't post back to itself.

    Controls:
    * You aren't handling the control's events.
    * The control has no dynamic or data bound property values (or they are set in code on every request).


Bookmarks

Posting Permissions

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