Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable ViewState for few controls and disable for others/page

When I disable ViewState for the page. It does not allow any other control to use ViewState .. even if I set EnableViewState="true" for that particular control ..

is it possible to enable ViewState for a control when ViewState is disabled for the page itself?

if not how can disable viewstate for controls on page except for few without specifying EnableViewState="false" explicitly .. typing the same into so many controls is hectic ..

like image 862
Zuhaib Avatar asked Oct 09 '08 10:10

Zuhaib


People also ask

Which property is used to disable enable ViewState of control at page level?

ViewState can be disabled for the whole Page i.e. all controls on the Page by setting the EnableViewState property to False in the @Page Directive.

What happens when ViewState is disabled?

If you disable ViewState, and a page needs it, the page will no longer work.

How do I turn off ViewState?

From the menu, select Settings. Then follow the path Privacy > Posts, and toggle the Hide Like and View Counts switch on.


2 Answers

If you set turn page's ViewState off, then there is no way for you to enable ViewState for specific components. This is because ViewState is serialzed recursively, so when if the Page is not allowing ViewState, it will not serialize the ViewState for any of it's child controls.

In answer to your question, if you don't want to explicitly turn ViewState off on individual controls, but want to keep some controls ViewState aware, the best way would be writing a small utility method which turns ViewState off for all controls (using recursion or otherwise). Then enable ViewState for the controls that you would like to enable ViewState for.

Alternatively, a middle ground and less forceful way may possible if controls are groups inside other container controls (such as Panel). You can disable ViewState for all controls inside a Panel by disabling ViewState of the Panel.

like image 171
Samuel Kim Avatar answered Oct 21 '22 09:10

Samuel Kim


If you set turn page's ViewState off, then there is no way for you to enable ViewState for specific components. This is because ViewState is serialzed recursively, so when if the Page is not allowing ViewState, it will not serialize the ViewState for any of it's child controls.it's child controls.

With the advent of ASP.NET 4 we have a new property called ViewStateMode that can be used to enable view state for an individual control even if view state is disabled for the page.

To make use of this new property you can either create a base page class that sets the ViewStateMode to Disabled or create a PageAdapter that does the same thing. There is no viewStateMode property in the web.config.

Here's the code for the page adapter:

using System.Web.UI;
using System.Web.UI.Adapters;

namespace Playground.Web.UI.Adapters
{
    public class PageAdapter: System.Web.UI.Adapters.PageAdapter
    {
        protected override void OnLoad(EventArgs e)
        {
            ViewStateMode = ViewStateMode.Disabled;
            base.OnLoad(e);
        }
    }
}

and here's the code for the browser file:

<browser refID="default">
    <controladapters>
        <adapter controlType="System.Web.UI.Page" adapterType="Playground.Web.UI.Adapters.PageAdapter" />
    </controladapters>
</browser>
like image 33
Georgios Politis Avatar answered Oct 21 '22 07:10

Georgios Politis