November 2008 - Posts
T4 templates have been a pretty popular topic lately. If you have no idea what they are, don’t feel bad, I didn’t either only a couple weeks ago! In a nutshell, it’s a simple template processor that’s built into VS and allows for all kind of cool code generation scenario. For a bunch of information about them, check out the following two blogs: GarethJ’s blog Oleg Sych’s blog The basic idea is that you can drop a hello.tt file into a project, and it will generate a hello.cs (or hello.anything) file via its template. The template syntax of .tt files is very similar to ASP.NET’s <% … %> blocks, except that they use <# … #> instead. For a cool example of what you can do with T4, check out this post from Danny Simmons, which shares out...
Dynamic Data has the concept of Page Templates, which are pages that live under ~/DynamicData/PageTemplates and are used by default for all tables. Recently a user on the forum asked whether they could use User Controls instead of Pages for those templates. To me, this means potentially two distinct scenarios, and I tried to address both in this post: Using routing : in this scenario, you still want all your requests to go through the routing engine, but have the user control templates somehow get used. The URLs here would still look identical to what they are in a default Dynamic Data app, e.g. /app/Products/List.aspx (or whatever you set them to be in your routes). No routing : in this scenario, you want the URL to go directly to a specific...
One user on my previous post on ProcessGenerateCode asked how he could associate a ControlBuilder not with a control, but with the page itself. There is in fact a way to do it, and it’s another one of those things that have never really been advertized. The trick is that instead of associating the ControlBuilder using the standard ControlBuilderAttribute, you need to use a FileLevelControlBuilderAttribute. Let’s walk through a little example. First, we create a custom page type with that attribute: [ FileLevelControlBuilder ( typeof ( MyPageControlBuilder ))] public class MyPage : Page { } We could do all kind of non-default thing in the derived Page, but I’ll keep it simple. Now let’s write the ControlBuilder for it: // Note that it must extend...
If you’ve ever written any non-trivial ASP.NET control, you’re probably familiar with the concept of a Control Builder. Basically, it’s a class that you associate with your control and that affects the way your control gets processed at parse time. While ControlBuilder has been around since the ASP.NET 1.0 days, a very powerful new feature was added to it in 3.5 (i.e. VS 2008). Unfortunately, we never had a chance to tell people about it, and a web search reveals that essentially no one knows about it! Pretty unfortunate, and obviously, the point of this post is to change that. :-) So what is this super cool feature? Simply put, it lets the ControlBuilder party on the CodeDom tree used for code generation of the...
More Posts