November 2005 - Posts

Why is the HtmlHead class sealed?
ASP.NET 2.0 gives us a Page.Title property, which we can set in code, or in the Page directive. Great! Unfortunately, I had a requirement so that whilst I'd be setting a portion of the title from the page, the rest would be pre-defined (ideally within the master page that I use). Obviously you can't fiddle the stuff in the server-side title tag, because the contents just gets overriden. So I thought I'd just extend the new System.Web.UI.HtmlControls.HtmlHead class and fiddle the Title property to add the required string to the end Except you can't. Because its sealed. Why? And does anyone have another way to do this?
Enhanced UK Developer Event Listings
I've now got some enhanced UK developer event listings up on Developer Fusion - check it out at UK Developer Events. Suggestions for improvements are always welcome. Made a nice use of the Google Maps API to do this... (sorry Microsoft, but the MapPoint service is way out of my price league!). Next on the cards will be setting up an extended RSS format for events (there doesn't seem to be one at the moment?) so we can pull information directly from UK user groups such as VBUG.

Watch this space!

If you run a user group or other developer (or IT-pro) related events in the UK, drop me a line and we'll get information about your upcoming events listed, along with a page for your organisation.
Multiple CSS Classes
I'm not sure I should really admit to not knowing this... but today was the first time I'd realised that the "class" attribute for elements within a HTML document can accept more than one class name. doh.

Update: Here's a simple example. With the following in your style sheet...

.align-c { text-align: center; }
.font-b { font-weight: bold; }


You can have something like <div class="align-c font-b"></div> to make the contents of the div tag bold and centre-aligned.
Caching Method Results in ASP.NET 2.0 using Delegates

Hmm. Talk about over-engineering. I don't think we really need generics at all, provided we're happy with a cast outside the method instead of inside it.

public delegate object MethodExecution();
public static object GetCachedMethod(string key, DateTime absoluteExpiration, MethodExecution method)
{
    if (HttpContext.Current.Cache[key] == null)
        HttpContext.Current.Cache.Insert(key,
            method(),
            null, absoluteExpiration, Cache.NoSlidingExpiration);

    return HttpContext.Current.Cache[key];
   
}
...
return (DataSet)GetCachedMethod(key, DateTime.Now.AddDays(1),
                delegate() { return SomeMethodThatReturnsADataSet(myParam); });

Caching Method Results in ASP.NET 2.0 using Generics & Delegates
Today I found myself coding a fairly familiar pattern - checking whether there was an entry in the ASP.NET cache with a particular key, if not, executing a method and adding the result to the cache, and either way, returning the result.

I wondered whether there was a "nice" way to do this using generics and anonymous delegates. This is what I came up with...

public delegate T MethodExecution<T>();
public static T GetCachedMethod<T>(string key, DateTime absoluteExpiration, MethodExecution<T> method)
{
    if (HttpContext.Current.Cache[key] == null)
        HttpContext.Current.Cache.Insert(key,
            method(),
            null, absoluteExpiration, Cache.NoSlidingExpiration);

    return (T)HttpContext.Current.Cache[key];
   
}

Now, to use this method, I could write the following to return a cached (by one day) result of the method SomeMethodThatReturnsADataSet.

return GetCachedMethod<DataSet>(key, DateTime.Now.AddDays(1),
                delegate() { return SomeMethodThatReturnsADataSet(myParam); });

I'm not sure whether this just makes things more obscure - any comments? :)

Re-setting Identity Column in SQL Server
Discovered something new today - normally I'd just use the TRUNCATE TABLE command in order to reset an identity column in a table within SQL Server. However, SQL Server doesn't let you do this if you've got foreign key constraints pointing at the table; so instead, I deleted all the rows using a standard DELETE statement, and then reset the identity with the following command:

DBCC CHECKIDENT (table_name, RESEED, new_reseed_value)

According to the docs, "The current identity value is set to the new_reseed_value. If no rows have been inserted to the table since it was created, the first row inserted after executing DBCC CHECKIDENT will use new_reseed_value as the identity. Otherwise, the next row inserted will use new_reseed_value + 1. If the value of new_reseed_value is less than the maximum value in the identity column, error message 2627 will be generated on subsequent references to the table."

So, as we've already had rows in the table, I called the function with new_reseed_value  to zero - hence the next row inserted into the table gets an identity of "1". Nice!
AJAX in ASP.NET
For those of you who missed my presentation on AJAX in ASP.NET at DDD II... or just want to see me mess up in my first demo again, you can now download a video of it from here. Enjoy! :-)
More Posts