如何使自定义控件的属性成为泛型类型?
我创建了一个自定义 ASP.NET 控件(源自 WebControls.TextBox
)。
我想向该控件添加一个类型的属性。
该属性实际上始终是某种类型的 enum 。 因此,当在设计器中时,我查看该控件的属性窗口 - 我希望能够通过从特定枚举中进行选择来为该属性分配值。 所以我想看到我作为..传递的枚举中的枚举器列表
(不是将编译的实际代码..只是为了展示我的意思):
我有2个枚举:
enum MyEnumABC
{
A,B,C
}
enum MyColor
{
Red,Blue,Green
}
我有这个控制:
public class MyTextBox<T> : TextBox
{
public T Classification
{
get { }
set { }
}
}
现在我创建具有以下控件的网页:
<Alex:MyTextBox runat=server id="alex" Classification=MyEnumABC.A></Alex:MyTextBox>
<Alex:MyTextBox runat=server id="alex2" Classification=MyColor.Red></Alex:MyTextBox>
问题是我可以在哪里实际将类型传递给该控件的构造函数? (因为页面类是调用控件构造函数的类。) 我实际上需要将 alex1 的类型设置为 MyEnumABC ,并将 alex2 的类型设置为 MyColor 类型。
第二个问题是我如何让 VS2008 在 HTML 设计器中支持这一点(这样当我在标签中键入分类时 - 它将打开写入枚举以选择值)和控件的属性页。
我希望你明白我的意思。
谢谢 。
I have created a custom ASP.NET control ( derived from WebControls.TextBox
).
I want to add a property to that control which will be of a type.
This property will actually always be some type of enum .
So when in the designer I look at the properties window of that control - I want to be able to assign value to that property by a selection from the specific enum .
So I want to see there the list of enumerators from the enumeration that I pass as ..
Example ( not actuall code that will compile .. just to show what I mean):
I have 2 enums :
enum MyEnumABC
{
A,B,C
}
enum MyColor
{
Red,Blue,Green
}
I have this control:
public class MyTextBox<T> : TextBox
{
public T Classification
{
get { }
set { }
}
}
Now I create a webpage which have following controls:
<Alex:MyTextBox runat=server id="alex" Classification=MyEnumABC.A></Alex:MyTextBox>
<Alex:MyTextBox runat=server id="alex2" Classification=MyColor.Red></Alex:MyTextBox>
The question is where can I actually pass the type to the constructor of that control ?
( since the page class is the one who calls the constructors of the controls.)
Where I actually need to set the type of alex1 to be of MyEnumABC , and the type of alex2 of type MyColor.
And the second question is how I make the VS2008 to support this in the designer of HTML ( so that when I type the Classification in the tag - it will open the write enum for selection of the value) and the property page of the control.
I hope you understand what I mean here.
Thanks .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设计器将支持枚举属性,而您无需执行任何特殊操作。 只需在控件的代码中指定属性,为其提供所有常用属性以允许其显示在属性窗口中,属性窗口将自动在下拉列表中显示枚举值。
The designer will support enum properties without you having to do anything special. Just specify the property in your control's code, give it all the usual attributes to allow it to be displayed in the property window and the property window will automatically display the enum values in a dropdown.