繁体   English   中英

如何在ASP.NET中创建和使用通用Web窗体用户控件?

[英]How to create and use a Generic Web Form User Control in ASP.NET?

我已经使用C#创建了一个Web表单用户控件。 我更改了用户控件的.cs文件(代码和设计器)以允许任何类型。

public partial class MyGenericControl<T> : System.Web.UI.UserControl
{
}

现在,我正在尝试像这样的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方法,但我认为在这里不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM