在 ASP.NET 中子类化 DropDownList [英] Subclassing DropDownList in ASP.NET

查看:25
本文介绍了在 ASP.NET 中子类化 DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 ASP.NET 中的内置 DropDownList 进行子类化,以便我可以向它添加功能并在我的页面中使用它.我尝试使用 UserControl 执行此操作,但发现它没有公开内部 DropDownList(我猜从逻辑上讲).我用谷歌搜索了答案,但找不到任何东西.

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 doesn't expose the internal DropDownList (logically, I guess). I've googled for the answer but can't find anything.

我已经编写了实际的类,并且可以从 DropDownList 进行子类化,但是我无法在我的 ASP.NET 页面中注册该文件并在源视图中使用它.也许我的班级缺少某些属性?

I've come as far as writing the actual class, and it's possible to subclass from DropDownList but I'm unable to register the file in my ASP.NET page and use it in the source view. Maybe I'm missing some properties in my class?

有什么想法吗?

推荐答案

您想在自定义控件中扩展 DropDownList...而不是在用户控件中.

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

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

Create a new Class Library Project called MyLibrary.

添加一个名为 MyDropDownList.cs 的类

Add a class called 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
    }
}
}

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

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

以及 web.config 中的标签前缀

And a tagprefix in your web.config

    <add tagPrefix="my" namespace="My.Namespace.Controls" assembly="MyLibrary" />

这应该允许您将其添加到 aspx/ascx 的

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

<my:MyDropDownList ID="myDDl" runat="server">
    ...
</my:MyDropDownList>

这篇关于在 ASP.NET 中子类化 DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆