简体   繁体   中英

Disable viewstate for all Label controls in web.config?

Is it possible to disable viewstate for a specific type of controls in web.config?

I know I can disable viewstate for all pages using <pages enableViewState="false"> , but what if I wanted to target just all Label controls?

One of the crud way, could be to iterate all controls recursively in the page and disable the view-state for label controls - obvious issue is that you need to do this early in page cycle and then in such case, you may miss the dynamically added controls.

The more elegant solution would be to create a custom control inheriting from say label control and disable the view-state (say in the constructor) - for example

public class MyLabel : System.Web.UI.WebControls
{
  Public MyLabel() 
  {  
    EnableViewState = false;
  }
}

(Mind you that in a robust implementation, you should ensure that view-state is disabled before it gets saved.)

Now, you can use ASP.NET tag mapping feature to map all label controls to your label controls from web.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM