Wednesday, April 26, 2006

Web custom controls - changing the base tag

In this blog entry "A detail the Webcontrol books don't teach you", Miguel Castro tells you how to override the default behavior of a web custom control and replace the SPAN that is generated with a DIV (or any other tag!), and points out the differences needed when your control inherits from System.Web.UI.CompositeControl rather than System.Web.UI.Control.

Amazing how Googling (in this case for "web custom control render div not span") can give you exactly the answer you were after!

Fixing ATLAS Control Extenders

ATLAS is a really nice piece of technology, allowing you to AJAX enable an ASP.Net page. The recently released ATLAS Control Toolkit provides a suite of ATLAS controls that are useful out of the box, and provide a good set of samples for developing your own.

Of course, the problem with using any third party control is that you may have trouble when the control doesn't work in quite the way you expect.

I've been having a problem with the Popup Control Extender recently. It's a control to let you use one control on an ASP.Net page to toggle the visibility of another - and being part of the ATLAS Control Toolkit, it's fully ATLAS enabled and so renders the behavior on the client.

The problem was that the designers of the toolkit didn't consider the case when the extender was used in a composite control that was repeated on a page. The toolkit renders markup on the page for behavioral properties exposed from the extender, but wasn't catering for when the extender was itself repeated as part of a composite control.

Fortunately, the source code for the Popup Control Extender comes with the ATLAS Control Toolkit itself, so I was able to fix the fault myself - score one for open / shared source!

I posted the full fix in the ASP.NET Forums - hopefully others will find it useful. I'm just made up to be able to contribute something back for a change!

Friday, April 21, 2006

Getting at HttpServerUtility

Here's the problem - you want to HTML encode some content in a class library that supports your ASP.Net application... You could write the routine to HTML encode a string yourself, or you could re-factor your code to do the encoding in the page code-behind like this:
public partial class DemoPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LabelControl.Text =
Server.HtmlEncode( LibraryClassInstance.Content );
}

// rest of the implementation goes here
}
But what if you're abstracting things so that a library object has to be able to render itself as HTML?

The problem is all about getting at the HtmlEncode method if you've not got a Server object handy. The Server object is actually a singleton instance of the HttpServerUtility class exposed by the ASP.Net environment from the HttpContext in which the page is running.

HOWEVER, the HttpContext class exposes a static method to return the current context instance, including the associated HttpServerUtility instance, so all you have to do in the library is the following
namespace demo
{
class LibraryClass
{
public string GetHtmlSafeContent()
{
return HttpContext.Current.Server.HtmlEncode(this.Content);
}

// remainder of the implementation goes here
}
}
Voila - HTML (and URL) encoding and de-coding at your fingertips.

Design-time goodness: ImageUrls

When you're developing web server controls, how often do you expose a Url or an ImageUrl property? Chances are that it's one of those tasks that happens 9 times out of 10, as adding image support to an existing or composite control is that common a task.

In this forum post, Steven Cheng from MS support gives a good code-snippet for a property that has the default ImageUrl designer applied to it. Unfortunately, he neglects to point out the assembly references needed to get it to work.

To use the standard designers requires you to reference the System.Design.dll assembly in your control library, and then to add the using statements highlighted in the sample below


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Design;
using System.Drawing.Design;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;

namespace DemoControls
{
[DefaultProperty("Caption")]
[ToolboxData("<{0}:CoolButton runat=server>")]
public class CoolButton : ImageButton
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
[Editor("System.Web.UI.Design.ImageUrlEditor, System.Design,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",typeof(UITypeEditor))]
public string RolloverImageUrl
{
get
{
String s = (String)ViewState["RolloverImageUrl"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["RolloverImageUrl"] = value;
}
}

// rest of the implementation goes here...
}


Adding this kind of design-time support is a snip, and makes those simple extended controls much more usable.

Tuesday, April 18, 2006

Source Code for the Built-in ASP.NET 2.0 Providers Now Available for Download

In his blog, ScottGu today announced:
Today we released the source code for the built-in ASP.NET 2.0 Membership, Role Management, Site Navigation, Session State, Profile, Web Events, and Web Part Personalization providers

This is a great thing - not only is Microsoft being much more helpful by providing sample code to let us develop (in this case) pluggable providers, but rather than releasing ????? code which just demonstrates the concepts, they're releasing the production code that we're already using.

The ASP.Net developer centre also great documentation on the Provider design pattern/model, and the individual provider types used by ASP.Net 2.0.

Finally, they've also released this under the Microsoft Permissive License (Ms-PL) - a license that allows and encourages the development of derivative works.

I for one can see myself using all of these types of providers at some point or other in my current project, so these samples are going to make my life much easier. Good stuff.

Thursday, April 13, 2006

VS2005 Web Application Projects hits RC1

I've blogged about this VS2005 add-in before, as it provides a much better migration experience for complex web applications.

But today Robert Shelton posted that the add-in has now reached RC1. This is a good thing, as a (nearly) production ready toolkit for migrating "old" ASP.Net 1.1 web applications is sorely needed.

You can read his blog post here and get the download here.

Moving a development team forward - "scrimages"

In this blog article, David L details how he implemented a "Developer training regime" at a client in order to move the team's .Net development skills forward and prepare them to migrate to ASP.Net 2.0.

Apart from the (slightly dubious) American Football metaphor, the concept is remarkably similar to the Developer-to-Developer training that Angel Solutions and Mandogroup have already implemented, and that I intend on implementing here at In4Tek.

The article makes a good read, however, and is a timely reminder about the value of your developer's skill-sets and keeping them up to date, but also serves as a reminder that the training should perhaps have rather more focus on the end goal (in this case of migration) than be open-ended as the Mandogroup training has been thus far.

Check the article out here:

http://blogs.msdn.com/davidlem/archive/2006/04/13/575344.aspx

Columbus would have been proud - extending the ATLAS

The ATLAS group have been busy - today they announced the release of the ATLAS Control Toolkit. From the introduction.
The 'Atlas' Control Toolkit is a collection of samples and components that makes it easier then ever to build and consume rich client-side “Atlas” controls and extenders. The toolkit provides both ready to go samples and a powerful SDK to simplify the creation and re-use of your own custom controls and extenders.
As always, the best way to evaluate at a toolkit like this is to look at the samples provided, and the ATLAS toolkit doesn't disappoint, with 9 new controls and extenders that cover some of the most common AJAX features you'd need to make a site provide a rich and dynamic experience.

My particular favorites include the PopupControl and the ReorderList - paired, these two would provide an editing experience for simple data (such as lookup table data, for example) almost as good as could be achieved with a Windows Forms client.

More later, but I can see myself using these, and the guides for developing your own ATLAS controls and extenders extensively over the next little while.

Wednesday, April 12, 2006

Mix-and-match Unit Testing

In this MSDN Magazine article, John Robbins presents a tiny little hack that could save a huge amount of time if you're migrating a .Net 1.1 application with NUnit tests to .Net 2.0 and Visual Studio 2005.

The hack basically involves using some compile tests to switch between NUnit and VS2005 native testing.

The code he cites was actually found in the Patterns & Practices group Composite UI block source and is remarkably simple, requiring just the following code at the head of every test class and a change from using [Test]attributes to using [TestMethod]attributes instead.
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif
Nice when you find a simple solution to an apparently intractable problem!

Tuesday, April 04, 2006

Developer Highway Code

My colleague Andrew Lightowler spotted this in the security section of the UK MSDN site today. The Developer Highway Code: provides a checklist of security issues that every .Net developer should keep in mind when planning and implementing a piece of software. From the introduction:
To build software that meets your security objectives, you must integrate security activities into your software development lifecycle. This handbook captures and summarises the key security engineering activities that should be an integral part of your software development processes.
At over 170 pages, it's fairly heavyweight, but well worth a read, particularly in concert with Writing Secure Code (which you can, of course, get from Amazon).

I do, however, wonder how well (or more likely badly) some of my old code would fare against these checklists!