Skip navigation

Code Neat Freak

The C2 Group

I am a bit of neat freak. My wife might disagree, but she doesn’t see the code that I write. I am constantly looking at new theories and design patterns that will make code more readable, reusable, and reliable. These code qualities are indispensable when working with a team of developers. Any one developer could be working on your code at any given time. Needless to say, I’m a pretty big fan of Onions.

Now that you know how much of a meticulous neat freak I am, let me give you a way to clean up your code. Recently, I was working on a project that had lots of paging. I mean lots. I had 8 templates to make and had 3 different styles of paging to implement. When I got to the first template I made an ASP.NET User Control and all was well. When I came to the second style of paging I wrote another User Control to handle that style. When I came to the third style I already had Paging.ascx, MobilePaging.ascx so what was the third one supposed to be called? AnotherPaging.ascx?


This is where the neat freak clean code stuff comes in. After analyzing the paging controls I realized they were the same! They did the exact same thing, they all changed the page, but they looked different. Why couldn’t I put them all in one user control? Then it hit me. I needed a way to allow the presentation portion of the control to be specified on each page but retain the functionality from the code behind. It just so happens that ASP.NET has a great way to do this.

If you’re reading this, I’m assuming you’re a developer and a .NET developer at that. I would bet you’ve used a Repeater at some point and had to specify data in a template. That was my answer: templates.

I converted the back end code to a class that inherits from the .NET WebControl class and added templates for the different areas that I needed:  FirstTemplate, LastTemplate, NextTemplate, SelectedTemplate, etc. Suddenly my three User Controls became one Templated Server Control, and I no longer had to duplicate code! This also gave me one Pager.cs file that I can easily add to other projects.

Making a Templated Server Control wasn’t all that easy; make sure you recreate the Control Hierarchy by overriding the CreateChildControls function with a dummy data set if necessary. When you have child Controls, such as LinkButtons, in your templates, .NET can’t find Controls that haven’t been added to the Control Hierarchy, and your event commands won’t get called.  I used the PagingInfo object from the previous request.

protected overridevoid CreateChildControls()
        var pagingInfo = ViewState["PagingInfo"] as PagingInfo;
        if (pagingInfo == null) return;
        _dataSource = pagingInfo;
        //Create the child controls from the given data set

The bottom line is that ASP.NET Server Controls can greatly reduce the amount of code you write and can streamline your process. I now have a Templated Server Control that can be used  for anything that needs paging on future projects so I never have to rewrite one! Now go write a Templated Server control and clean up that code, and if you didn’t get my joke about Onions… I’ll get back to it in the next blog post.



Like what Drew has to say? Check out what he had to say about our last Ektron training session here!