Enable ViewState for few controls and disable for

2020-02-23 07:40发布

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

8条回答
啃猪蹄的小仙女
2楼-- · 2020-02-23 08:41

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>
查看更多
Summer. ? 凉城
3楼-- · 2020-02-23 08:41

You can also subclass the built-in controls, and in your subclass set the EnableViewState property to false.

查看更多
登录 后发表回答