How do you see markup syntax of ASP.NET controls with generics?

Reading this post made me thinking how exactly markup for a generic control should look like. It would be nice to have something that is SGML-compiliant syntax so we won’t have to special case anything in our parser. For example

<vc:SomeGenericControl<SomeObjectType> runat=”server” />

obviously is not SGML compliant because of nested angle brackets. It is difficult to parse this kind of constructs since it not different from

<input <div> some text />

which is plain ugly invalid HTML which has to be squiggled. Another one, proposed in the abovementioned post,

<vc:SomeGenericControl:SomeObjectType runat=”server” />

is better, but still is not quite compliant since only namespace:element pairs are allowed (W3C theoretically allows colons in element names, but namespace spec forbids them. However, it is relatively simple to add functionality to the HTML parser and make it recognize this kind of constructs.

Dot (period) is allowed in element names and this one

<vc:SomeGenericControl.SomeObjectType runat=”server” />

is a valid construct and in fact, XAML uses it extensively. However, now we will experience difficulties specifying more than a single type, i.e. in

<vc:SomeGenericControl.SomeObjectType1.SomeObjectType2  runat=”server” />

it is unclear if SomeObjectType1.SomeObjectType2 should be parsed as

<SomeObjectType1,SomeObjectType2> or as <SomeObjectType1.SomeObjectType2>.

So syntax with semicolons appears to be better in this fashion:

<vc:SomeGenericControl:SomeObjectType1.SubType1:SomeObjectType2.Type2  runat="server" />

Another way might be (as long as we accept that file is no longer SGML/XML compilant) to use braces

<vc:SomeGenericControl(SomeObjectType1.SubType1, SomeObjectType2.SubType2)  runat="server" />

You are welcome to post your ideas in the feedback!

Comments (32)

  1. Moshe Eshel says:

    Why not just add another Attribute to the Element (or even a few) that would be both XML/SGML comliant and also serve the purpose – unless I’m missing something basic.


    I wish I had a blog here to be able to put my URL above 🙁

  2. Mikhail Arkhipov (MSFT) says:

    Attributes typically map to control properties, but yes, it is possible to add attributes that specifically target generics types.

  3. Braces. Easy enough to read.

  4. in my opinion, whatever the end result is, please don’t break from the standard. At the end of the day, it is the standards that make sure everything stays within reason and you’re not left with a proprietary mess that no one will touch with a ten foot pole.

    how about using a double underscore to work that out?

    <ns:control__type runat="server" />

  5. Bill Trowbridge says:

    > It would be nice to have something that is SGML-compiliant syntax so we won’t have to special case anything in our parser.

    It’s not just nice. I’d say it’s an absolute requirement that any syntax be completely SGML-compliant. Otherwise you’d be breaking both the parser and the standard. (I prefer Moshe’s suggestion.)

    > … syntax with semicolons …

    I’m not sure if you really mean semicolons, or colons:

    semicolon = ;

    colon = :

  6. lexp says:

    <vc:SomeGenericControl(SomeObjectType1.SubType1, SomeObjectType2.SubType2) runat="server" />

    for me is the best one.

    To be fully XML compliant, consider the following:

    <vc:SomeGenericControl-SomeObjectType1.SubType1-SomeObjectType2.SubType2 runat="server" />

  7. lexp says:

    <vc:SomeGenericControl-SomeObjectType1.SubType1-SomeObjectType2.SubType2 runat="server" />

    by the way is very easy to parse. And in future it can be ported to fully XML-complant ASP.NET version (will it be?).

  8. That looks fine to me – I’d definitely go with an SGML-compliant syntax. Any idea if something like this stands any chance of being implemented for the ASP.NET 2.0 parser? 😉

  9. Mikhail Arkhipov( (MSFT) says:

    I like the proposal with dots and dashes. I am always trying to steer file formats towards standard ones since it greatly simplifies my team’s life as we don’t have to special case yet another format So you don’t have to preach XML to me 🙂

    Underscores, I think, are less convenient since element names with underscores are fairly common. Using additional attributes probably not very convenient since we want to be able to specify generics on attributes as well as any control property can be generic. Therefore, notion of the type has to be in the element or attribute name.

  10. Eric Newton says:

    Are we over thinking this? Generic webcontrols? I’m having a hard time imagining why you’d want to define a Webcontrol or Form control as a generic? …to strongly type the Tag property (on Forms controls)?

    Personally, I dont think this is an appropriate use of generics. Controls should be concrete.

  11. I have to agree with Eric Newton on this, at the page level, generics seem feasible, in fact I’m currently using them that way, as a collection of controls. But to have a ASP.NET control that is a collection of any/all controls seems a bit much. Plus doesn’t (sarting with the Page class) already provide a control heirarchy?

  12. how does Xaml markup generics?

    further, it might help if you could give a slightly more concrete example of when you might use this…

    how about using a namespace qualifier to denote the concrete type information?

    like, assuming that BindableList is a generic control, which can contain typed references to other controls that will be bound to…

    <BindableList Generic:Types="CheckBox, Button" DataSource="…" />

  13. <vc:SomeGenericControl$SomeObjectType$ runat="server" />

    or even

    <vc:SomeGenericControl$string$MyType.SubType runat="server" />

    you got the idea 🙂

  14. Funny, I was thinking about the exact same question last week. I came up with the same dash suggestion that’s been suggested before because it’s readable, xml-compliant, easily parsable and can’t conflict with existing controls.

    The applications I can immediately see are strongly typed textboxes:

    <asp:TypeBox-DateTime runat=server value="8/21/2004"/>

    and strongly typed grid columns.

    These two alone would justify the feature to me.

  15. Eric Newton says:

    I like where you’re going with the strongly typed grid columns. But the "TypeBox-DateTime" doesnt make sense…

    Consider that the "TypeBox" would theoretically have to validate the text as DateTime, and having the TypeBox’s Value property be typed to DateTime is nice, but I still think that CONTROLS should always be concrete…

  16. Derek Harmon says:

    What representation would be used when one of the types in the generic is itself a generic? For example, if I had the conventional template programming example of a Dictionary&lt;string,Iterator&lt;DateTime&gt; &gt;? Dots and dashes (which of the proposals I like best as ‘-‘ is allowed in XML names, but not identifier names) seem to only give us one level of genericism.

    I see plenty of valuable use cases for server control generics, by the way. Generics are not only for strongly-typed collections! They can be leveraged to construct entire business frameworks. Imagine, if you will, an insurance company’s DataGrid that’s a generic based on a WholeLifePolicy, a VULPolicy, a TermLifePolicy, etc. Each of these DataGrids could now expose method and property signatures made more type-aware and reusable by being a generic. This makes them great candidates for being readily genericized based on the type of the company’s business objects.

  17. Keith J. Farmer says:

    How would you handle generic methods, eg


    Perhaps some form of typeDef/methodDef declaration, though specifying the legal generic options at compile-time would disallow the runtime option of


    Of course, another option is to break with XML/SGML. I wouldn’t relish that option at all.

    Could anything be done with BindableList[[Checkbox, Button]](bar, baz)?

  18. Wilco B. says:

    Generics in a markup language

  19. Mikhail Arkhipov (MSFT) says:

    Remember, each of the control properties may also be generic. If we add attributes that specifies types for the control class, we will also have to add attributes to specify types for every control property that employs generics. The list of attributes may then become very long.

  20. Wilco B. says:

    The control’s properties can not be generic, but they can occur in generic types and use the type parameters from the enclosing type. I think what you mean is that the children of a generic control may also be generic. This means that you have to define the type parameters just like you would for its parent.

    Instead of attributes, using elements might be a more readable/flexible solution.

  21. Since the release of .NET 2.0 and ASP.NET 2.0 I was very disappointed that new language power of C# 2

  22. Since the release of .NET 2.0 and ASP.NET 2.0 I was very disappointed that new language power of C# 2

  23. Did you ever want to have a Repeater&lt;Customer&gt; control on your page? And its events were all strongly

  24. Steven Smith says:

    I just published an article on ASPAlliance that shows a few different techniques for binding a DropDownList

  25. Steven Smith says:

    I have three things that have been on my wish list for ASP.NET and/or Visual Studio that I’m curious