gpt4 book ai didi

c# - 在 ASP.NET 中子类化 DropDownList

转载 作者:太空狗 更新时间:2023-10-29 22:11:41 26 4
gpt4 key购买 nike

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

我已经编写了实际的类,并且可以从 DropDownList 进行子类化,但是我无法在我的 ASP.NET 页面中注册该文件并在源 View 中使用它。也许我在类里面遗漏了一些属性?

有什么想法吗?

最佳答案

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

创建一个名为 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
}
}
}

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

还有你的 web.config 中的标签前缀

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

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

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

关于c# - 在 ASP.NET 中子类化 DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/232934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com