在ASP.NET中对DropDownList进行子类化

dea*_*ime 5 c# asp.net user-controls subclass drop-down-menu

我想在ASP.NET中继承内置的DropDownList,以便我可以为它添加功能并在我的页面中使用它.我尝试用UserControl做这个,但发现它没有暴露内部DropDownList(逻辑上,我猜).我已经google了答案,但找不到任何东西.

我已经编写了实际的类,并且可以从DropDownList继承子类但我无法在ASP.NET页面中注册该文件并在源视图中使用它.也许我错过了班上的一些房产?

有任何想法吗?

Eoi*_*ell 16

您希望在自定义控件中扩展DropDownList ...而不是在usercontrol中.

创建一个名为MyLibrary的新类库项目.

添加一个名为MyDropDownList.cs的类

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

编译库后,可以在Web应用程序中添加对它的引用.

和web.config中的tagprefix

    <add tagPrefix="my" namespace="My.Namespace.Controls" assembly="MyLibrary" />
Run Code Online (Sandbox Code Playgroud)

那应该允许你将它添加到你的aspx/ascx中

<my:MyDropDownList ID="myDDl" runat="server">
    ...
</my:MyDropDownList>
Run Code Online (Sandbox Code Playgroud)