Subclassing DropDownList in ASP.NET

后端 未结 2 394
时光取名叫无心
时光取名叫无心 2020-12-18 07:41

I want to subclass the built-in DropDownList in ASP.NET so that I can add functionality to it and use it in my pages. I tried doing this with a UserControl but found that it

2条回答
  •  既然无缘
    2020-12-18 08:43

    You want to extend DropDownList in a Custom Control... not in a usercontrol.

    Create a new Class Library Project called MyLibrary.

    Add a class called MyDropDownList.cs

    namespace My.Namespace.Controls
    {
    [ToolboxData("<{0}:MyDropDownList runat=\"server\">")]
    public class MyDropDownList: DropDownList
    {
        // your custom code goes here
        // e.g.
        protected override void  RenderContents(HtmlTextWriter writer)
        {
            //Your own render code
        }
    }
    }
    

    Once you compile your library, you can add a reference to it in your web application.

    And a tagprefix in your web.config

        
    

    That should allow you to add this to your aspx/ascx's

    
        ...
    
    

提交回复
热议问题