Stupid Skinning Trick

I meant to post about this, oh, three months ago? I guess things got away from me.

You know those lists that Community Server offers for blogs? The ones I've got on the right side of my skin for blogs that I read and sites that I find myself at more than I should? Were you aware that those aren't sorted? This can be a bit misleading because they're sorted in your Control Panel, but on the page itself, they appear in whatever order they were originally entered, just like that image to the right.

Thankfully, we can address this with a very small amount a C#. The skin file that contains the UI for CS's lists is Skin-LinkList.ascx. The general structure of this file is a pair of nested Repeater tags. The outer Repeater iterates over your link categories (ID="Categories"), while the inner Repeater iterates over the links themselves (ID="Links"). Using a couple of strategically-placed ASP.NET event handlers, we can sort the contents of one or both of these Repeaters before they begin iterating. I only sort the inner Repeater in my skin, so that's the example I'll give.

The first thing you need to do is to hook the DataBinding event on the repeater. Locate the Repeater with the ID "Links" and add a reference to your event handler:

<asp:Repeater ID="Links" OnDataBinding="Links_DataBinding" runat="server">

Now you just need to add the Links_DataBinding method itself:

<script runat="server">
 
    protected void Links_DataBinding( object sender, EventArgs e )
    {
        Repeater links = sender as Repeater;
        if ( links == null )
            return;
        
        // The list of links is provided as an ArrayList. Note that is is extremely 
        // possible that this will change in future revisions of CS. This becomes a
        // possible breaking point for this skin.
        ArrayList items = links.DataSource as ArrayList;
        if ( items == null )
            return;
        
        // Sort the items using their default comparer. They're all strings, so 
        // this is a straightforward sort.
        items.Sort();
    }
    
</script>

This is really, really simple. The data source being passed to the Repeater is an ArrayList of strings, which are sorted easily enough. We get access to the data source (using the necessary safeguards to ensure that we don't blow anything up in the name of something so trivially cosmetic) and simply call ArrayList.Sort(). That's it!

Interestingly enough, if you look in the CS database schema, you'll notice that the table that contains links (cs_Links) also has a column called "SortOrder". I did some digging around in the source code, and I don't believe that this field is currently referenced anywhere in the application. I assume that it could be used as the basis for a control panel modification that would allow you to reorder your links on a whim, should anyone feel like going that far.

Comments

Knuckles Dawson said:

Yeah, that's pretty cool. Unfortunately, since I'm on Movable Type, my KD Endorsement list (where I put you second from the top) is manually entered, but gets the job done. Thanks for throwing me in there on such short notice. Oh, a little heads up, a little birdie told me that my site traffic is going to skyrocket early next week, which may trickle to that particular list. Just wanna give you a head's up.

Community Server Daily News said:

news of the day a grab bag for what's happening in Community Server BlogMailr. Get the buzz started.

wahooga.com said:

I had an email today from Dennis asking about the date display on my blog. It got me thinking that others

Daily News Faq List said:

I can't speak for everyone who reads the Daily News, but I have seriously missed The Blogfish! Justin

Community Server Bits said:

I can't speak for everyone who reads the Daily News, but I have seriously missed The Blogfish! Justin