如何在ASP.NET中创建和使用通用Web窗体用户控件
本文关键字:Web 窗体 控件 用户 ASP NET 创建 | 更新日期: 2023-09-27 18:24:00
我使用C#创建了一个Web窗体用户控件。我已经在用户控件的.cs文件(代码和设计器)中进行了更改,以允许任何类型。
public partial class MyGenericControl<T> : System.Web.UI.UserControl
{
}
现在我正在尝试将它用在像thi这样的ASP.NET表单中
<uc1:MyGenericControl runat="server" id="MyGenericControl1" />
但我不知道如何将我需要的类型发送到控件。
感谢您对这个问题的任何建议或帮助。
我会把我的回复放在评论中,因为我还没有答案,但我没有50个代表,所以我不能评论。
这是处理用户控件的一种非常有趣的方式。我想问你的是,你想用这个来实现什么?我们需要知道,因为您的解决方案可能不涉及接受任何类型的用户控件,就像Stilgar所说的那样。
您可以使用枚举器/属性组合来完成此操作。。以下是我的想法:
<uc1:MyGenericControl runat="server" id="MyGenericControl1" myControlType="DropDownList" />
public partial class MyGenericControl<T> : System.Web.UI.UserControl
{
public enum ucType : ushort
{
DropDownList = 1,
TextBox,
Button,
Etc
}
private ucType _controlType;
public ucType myControlType
{
get{return _controlType;}
set{ T = value; } /* Somehow set T to the value set in ASP.
In this example, value would be "1" so it would throw an error, but you get the idea. */
}
}
这比完整的答案更具启发性和发人深省,因为我不知道是否有可能动态设置类类型(尤其是你目前正在工作的类类型)。有一个Convert.ChangeType
方法,但我认为它在这里不起作用。