简体   繁体   中英

Deriving annoying ASP.NET class

I would like to derive a new class from System.Web.UI.WebControls.WebControl , however I think when you derive a class you dont inherit it's constructors.

The difficulty this causes is that one of the constructors for this class has a parameter of type System.Web.UI.HtmlTextWriterTag , which defines the html tag of the control/element created, and there is no way to set this after the class has been constructed/initialized.

Has anyone got any suggestions on how to work through this?

This should help: A Crash Course on ASP.NET Control Development: Deriving New Controls from Existing Classes

just follow this crash course and you will see how simple it is.

Like this?

public class MyWebControl: WebControl
{
    public MyWebControl(HtmlTextWriterTag tag) : base(tag) 
    {

    }               
}

You need should add the same constructors to your own type. And then use :base(...) to call the base constructors.

public MyType(ParamClass a)
    :base(a)
{
}

You can also use non matching signatures between your type and the base type, as long as the signature of :base(...) matches the signature of the base type constructor. But you can only use expressions to define the value of the parameters passed to it. So you might want to use some static helper methods.

When you derive a base class, the base class constructor will also be derived. You can pass the HtmlTextWriter type to the derived class ctr and in the derived class define the ctr as DervCtr(HtmlTextWriter) : base(HtmlTextWriter)

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