0

I have created a Web Form User Control using C#. I have changed in the .cs files (code and designer) of the user control to allow any Type.

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

Now I'm trying to use it in a ASP.NET form like thi

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

But i don't know how to send the type I need to the control.

Thanks for any suggestion or help to this question.

l2mt
  • 550
  • 2
  • 7
  • 20
  • possible duplicate of [Syntax for generic (i.e. ) web user control](http://stackoverflow.com/questions/16316469/syntax-for-generic-i-e-t-web-user-control) – MethodMan Sep 22 '14 at 15:43
  • 1
    Since I do not think this is possible maybe you can describe your use case so we can suggest the best workaround. – Stilgar Sep 22 '14 at 15:44

1 Answers1

0

I would put my response in a comment, because I don't yet have an answer, but I don't have 50 rep so I can't comment.

This is a very interesting way of handling a User Control. My question to you is, what are you trying to accomplish with this? We need to know, because your solution may not involve having a User Control that accepts any type - like Stilgar said.

You might could use an enumerator / property combo to accomplish this.. here's what I've come up with:

<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. */
    }
}

This is more suggestive and thought-provoking than a full answer, because I don't know if it's even possible to dynamically set a class type (especially one that you're currently working in). There is a Convert.ChangeType method, but I don't think that would work here.

Mike
  • 51
  • 8