Syntax for generic (i.e. <T>) web user control
Asked Answered
M

3

5

Assuming I have made a web control as follows:

public class TestControl<T> : WebControl
{
    ...
}

Is there any way to place that control on an .aspx page without having to do it via code? I really want to be able to do something like:

<controls:TestControl<int> runat="server" />

But as far as I can tell there is no way of me passing in the generic parameter. I've tried searching on the web and found this http://forums.asp.net/t/1309629.aspx, which appears to be exactly what I'm after, but no one seems to grasp what the guy wanted, and I can't find anything similar on StackOverflow.

Megalomania answered 1/5, 2013 at 10:28 Comment(1)
possible duplicate of Generic ServerControl syntax?Whither
C
5

No. There's no way to do that.

Chokebore answered 1/5, 2013 at 10:29 Comment(11)
Not even in .net 4/4.5/etc.? I'm stuck with 3.5 :(Megalomania
No, and I have never needed to do such a thing.Chokebore
Thanks, but why not? Seems like a very useful and valid thing to do.Megalomania
You're asking me why I've never needed a particular feature that doesn't exist? Think about that.Chokebore
But didn't you want it before you knew it didn't exist? Obviously if you knew it didn't exist first then yes, fine.Megalomania
I'm with John here; I can't even recall ever having the notion that this would be a good idea, let alone need it.Melitamelitopol
@GrantThomas: A strongly typed Repeater would be nice.Whither
@GeorgeDuckett To what end? Yes, a DataSource of type T would suit my desires for strongly typed stuff, but it would still be highly unnecessary in the context of an ASP.NET page where the type of the object instance will be well known at the time.Melitamelitopol
@GrantThomas: I agree it's minor in this case but really you've answered your question in your comment (strongly typed stuff). It's just another thing that could turn a runtime exception (caused by a silly mistake) into a compile time one.Whither
I agree that it would be a very useful feature. An example of usage would be a custom RadioButtonList whose options were settable from an Enum type injected generically, and whose presently-selected value could be expressed as an enum of that type, without having to do a cast or having to create boilerplate properties that convert the SelectedValue of the RadioButtonList into a value of the type defined by the enum. So, @ John Sauders et al, just because you've never needed a particular feature (or realised that you did) doesn't mean it won't be useful and valid for someone else!Py
In particular, see "Strongly Typed Data Controls" in "What's New in ASP.NET 4.5 and Visual Studio 2012"Chokebore
M
4

Nope. Your best bet might be to use that as a base and derive more direct controls from it, such as a TestIntControl, a TestStringControl and whatnot. I know this defeats the purpose of pure genericism, but you have few other options. You could then use these types in places where you need explicit markup, and still have the flexibility of the underlying type in more dynamic pages.

Melitamelitopol answered 1/5, 2013 at 10:31 Comment(0)
J
0

You can just make the generic type abstract, and inherit a concrete type that you can then place on the page. On one hand, this is a bit more code, but it does also allow you to customize the type by calling the base constructor.

public abstract class MyGenericControl<T> : WebControl {
   ...

   public T SomeStronglyTypedProperty { get; set; }

   protected MyGenericControl(...) {
      ...
   }

   ...
}

public sealed class MyConcreteControl : MyGenericControl<SomeType> {
   public MyConcreteControl()
   : base(
      ...
   ) {
   }
}

in your markup:

<%@ Page ... %>
<%@ Register assembly="MyAssembly" namespace="MyNamespace" tagPrefix="abc" %>
<asp:Content ...>
   <abc:MyConcreteControl id="myConcreteControl" runat="server" />
</asp:Content>

and then in your code behind:

...
SomeType value = GetAValue();
myConcreteControl.SomeStronglyTypedProperty = value;
...
Josefajosefina answered 6/3, 2015 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.