简体   繁体   中英

Can a class inherit from both System.Web.UI.Page and System.Web.UI.UserControl?

I have a class that stores ViewState in SQL Server (thanks Adam Weigert for the code). It seems to work for my web pages, but user controls within the page don't cause a ViewState save when they should. The code I got from Adam inserts a base class beneath System.Web.UI.Page. I think I need this code to also inherit from System.Web.UI.UserControl so that the user controls will also fire this code, but VS 2010 says:

Class 'SqlViewStatePage' cannot have multiple base classes: 'System.Web.UI.Page' and 'System.Web.UI.UserControl'

All of my web pages inherit from BaseWebPage which now inherits from SqlViewStatePage. All of my user controls inherit from BaseUserControl which I want to inherit from SqlViewStatePage, but this won't work.

How can I pull this off?

Shift the logic for " Stores ViewState in SQL Server " in a new .cs class. Access this class in Control and page

We narmally place three files in our architecture.

  1. Base Page - This file inherits the System.Web.UI.Page. Further, we inherit the Base Page in our aspx pages.

  2. Base Control - This file inherits the System.Web.UI.UserControl. Further, we inherit the Base Page in our User Controls.

  3. Base Class - We write the common functionality static/non static Data Members/Member Functions

In .Net we can't use multiple inheritance so you can't build a class which inherits from UserControl and Page in the same time.

I read a bit about SqlViewStateUserControl and it seems that was a solution for the .Net 1.0 and 1.1 . You are using VS 2010, you can benefit then by the PageStatePersister class. Inherit this class and implement it to save the viewstate to the database (this mechanism was introduced with .Net 2.0, I'm sure you'will find many implementations of it).

Not directly addressing your question but I played with overriding SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium years ago on a large project for a company that rented crains. The technique was fraught with issues especially when using the asp.net AJAX library and we ultimately abandoned the technique. Partial postbacks failed to restore ViewState.

So if this is why / how you are storing ViewState to SQL, you may have issues that need to be overcome.

Saving Viewstate on Server

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