2014-09-22 70 views
0

我用C#创建了一个Web窗体用户控件。 我已更改用户控件的.cs文件(代码和设计器)以允许任何类型。如何在ASP.NET中创建和使用通用Web窗体用户控件?

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

现在,我尝试使用它在ASP.NET表单一样THI

<uc1:MyGenericControl runat="server" id="MyGenericControl1" /> 

但我不知道如何发送我需要控件的类型。

感谢您对此问题的任何建议或帮助。

+0

[Syntax for generic(ie )web user control](http://stackoverflow.com/questions/16316469/syntax-for-generic-iet-web-user-control) – MethodMan 2014-09-22 15:43:28

+1

因为我没有认为这是可能的,也许你可以描述你的用例,所以我们可以建议最好的解决方法。 – Stilgar 2014-09-22 15:44:44

回答

0

我会把我的回应置于评论中,因为我还没有答案,但我没有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方法,但我不认为这会在这里工作。

相关问题