RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Web Control Enhancements in ASP.NET 2.0 : Page 6

ASP.NET 2.0 does not break any existing controls from 1.1, but it adds a whole bunch of new ones, as well as several new technologies for control development.

Web Resources
Web Resources are embedded files that get compiled into the DLLs of your controls. Windows Forms control developers have had this capability for a long time but now it finally comes to the Web. Imagine developing a control that relies on certain images for proper rendering. It would be extremely useful to be able to simply deploy the control's DLL file without also having to deploy the external image files. Check out Microsoft's new Tree Web control and you will see this in action. The best candidates for using Web Resources are image files, though I can also use them for JScript files.

To show you how this works, I'm going to add an image of a mailbox to the left of the heading in my EmailContact control. If you noticed Figure 4, there is already an empty image to the left of the "CONTACT US" heading caption. I did this simply by creating an Image child control and adding it to the control hierarchy in the proper place. In fact, I also created a property called ShowMailIcon to allow the page developer to hide or show the image.

Using a conventional approach you could create a HeadingImageUrl string property and set its value in the Image control's ImageUrl property. This would allow the page developer to set this image to whatever he or she wants. I agree with this approach, and I've incorporated it into this control. However, I've taken it a step further. If the HeadingImageUrl property is not empty I use it to fill the Image control's ImageUrl property as I just described. But if the property is empty, I do something very cool.

Using Web Resources, you no longer have to worry about distributing images with your controls.
I have a mailbox image in my Web control project called mailbox.gif. I will set the BuildAction property of the image to EmbeddedResource (I can do this from the property browser).

Next I need to access my project's AssemblyInfo.vb file. In ASP.NET 1.1, this file was located in my project's root folder but in ASP.NET 2.0 it is under the My Project folder. Not only that, but I have to click on the "Show All Files" button at the top of the property browser to access this file. At the bottom of the AssemblyInfo.vb file, I need to add the following line:

   <Assembly: WebResource("mailbox.gif", "image/gif")>
This identifies the embedded file as an available Web Resource for later extraction.

To extract the file, I use the GetWebResourceUrl method in the Page.ClientScript object. As stated earlier, I want to give the page developer the ability to override the image using my property so I code the extraction as follows:

   If Me.HeadingImageUrl <> "" Then
      imgMail.ImageUrl = Me.HeadingImageUrl
      imgMail.ImageUrl = Page.ClientScript.GetWebResourceUrl( _
         Me.GetType(), "mailbox.gif")
   End If
This really is my favorite new Web control feature because I tend to use little images here and there to spiff up my controls. Now I can embed them and simply distribute my control's DLL file only.

Testing the control will display what's in Figure 5.

Figure 5: Complete control with embedded image.
The Web Form uses an HTTP handler to retrieve the image and render it to the browser. The possibilities are endless with this one.

I hope you've enjoyed this tour. ASP.NET 2.0 brings many cool things to Web development with the introduction of master pages, themes, data sources, and other great features. In fact, you can embed callbacks (a new page feature) into controls as well, giving them AJAX functionality for better response time and a richer client experience. Unfortunately, that topic deserves its own article so I couldn't cover it here. The new control development features I have shown you here have not received the publicity they deserve, but hey, that's why I'm writing. As you can see, most of these are for design-time experience, but I've always preached that making your controls intuitive to the page developer is equally as important as making them useful and functional. If they're not easy to use, then you may not ever get to see them in action.

You can download the full version of EmailContact. Look for the presentation downloads and grab the one called "Web Control Enhancements in ASP.NET 2.0." For further study, check out the APress book, Pro ASP.NET 2.0 in C# 2005, by MacDonald and Szpuszta. It's a great complete book on ASP.NET 2.0 and has a couple of good chapters on Web controls where the authors cover many of the things I taught you here. A VB.NET version of the book is also available.

Miguel A. Castro is President of InfoTek Consulting Group, Inc., a professional consulting firm that specializes in architecting, designing, and developing solutions using Microsoft .NET technologies. His VB background goes all the way back to 1.0. Miguel's focus for the last couple of years has been the .NET Framework and languages; he is fluent in both VB .NET and C#. His programming experience goes back 20 years when he started on TRS-80s, Apple IIs, and Atari 800 computers. Miguel lives in Lincoln Park, New Jersey with his wife Elena and his daughter Victoria.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date