开发者

Converting ASP Web Form to User Control

I have a Web Form page that is called Search.aspx. I need to render the form in other places so I'd like to convert the Web Form to a User Control, but my Search.aspx.cs inherits a custom Page class which inherits System.Web.UI开发者_如何学JAVA.Page. Since I C# doesn't support multiple inheritance, I'm unable to inherit both my custom Page class and System.Web.UI.UserControl class in my User Control Search classs (Search.ascx.cs) class.

I was wondering if there was a way around this without having to create a second custom page class for the UserControl.

Thanks in advance.


What does the Search form need from the inherited custom Page class? You could design your search form so that it is nothing but a form of inputs and public properties that expose the input values. It's hard to say what the best answer is without more information about what value the custom Page class provides. Typically, controls won't need to know much about their parent page.


Depends on the rest of the architecture.

Simplest route is to change your inheritance to UserControl. If you're lacking some methods in your custom Page base class, you can duplicate them in your UserControl custom base class.

If you're trying to keep it DRY, refactor those custom methods into a class that provides those methods to any Page or UserControl class. You would be using composition rather than inheritance, which can be a better route to follow if your inheritance tree gets deep.

Another option is to extract an interface from your custom Page class. I don't think this would be a good idea. First, you most likely aren't needing to treat your Pages and your UserControls exactly the same (polymorphism). Second, you'll still have to implement the interface in both custom classes, so you end up breaking DRY anyhow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜