【发布时间】:2021-01-31 12:32:09
【问题描述】:
我正在尝试创建自己的下拉列表以在网页上使用,但我无法让它按照我想要的方式工作。
在最基本的形式中,我可以创建一个类,从 System.Web.UI.WebControls.DropDownList 继承,在页面上注册它,添加标记,它至少会在页面上显示一个控件:
我的班级:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace CustomDropdown
<DefaultProperty("Text"), ToolboxData("<{0}:DropDownListWithAttributes runat=server></{0}:DropDownListWithAttributes>")>
Public Class DropDownListWithAttributes
Inherits System.Web.UI.WebControls.DropDownList
End Class
End Namespace
页面注册:
<%@ Register TagPrefix="myControls" Namespace="CustomDropdown" %>
标记:
<myControls:DropDownListWithAttributes ID="ddlMyTestCustomDDL" runat="server"></myControls:DropDownListWithAttributes>
但就是这样。这是我所能得到的。我根本无法从后面的代码访问它。就好像它不存在一样。我需要能够用项目、触发事件等填充它...普通下拉列表可以做的所有事情。
所以我没有获得正常的 ddl 功能,我无法从后面的代码访问控件,添加任何类型的事件都会破坏东西......我不知道如何使这项工作发挥作用:(
【问题讨论】:
-
您是否在创建此自定义控件后尝试清理并重新编译您的项目?
-
是的。我建造/重建/清理和重建。我想我只是不明白创建这些类型的服务器控件的整个过程以及它们有什么能力和没有能力......叹息
-
自定义控件是否在单独的项目中?如果是这样,它可能需要在
Namespace CustomDropdown声明之前使用<Assembly: TagPrefix("CustomDropdown", "myControls")>。除了ToolboxData属性之外,它还需要Imports System.Security.Permissions和AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal)属性。 -
我把它和我的网页放在同一个项目中。但我想我会试一试并将它移到它自己的项目中。我像 Andrew 建议的那样添加了程序集和安全性,并修复了它,但它仍然无法在后面的代码中访问。我清理,重建了十几次,但没有运气。所以我开始从安德鲁的建议中删除一些东西并将其添加回来......一直在清理和重建。突然间,我在设计器文件中找到了一个很好的参考,并且在后面的代码中无法访问它。
-
您说它在您的代码隐藏中无法访问,但您能否在标记中添加“OnSelectedIndexChanged”偶数处理程序,并为您生成代码隐藏代码?
标签: asp.net vb.net drop-down-menu webforms web-controls