Add a daily Dilbert Comic with a Custom Control in VB.NET
This article describes the construction of a custom control used to display a daily Dilbert comic on a site.
This article describes the construction of a custom
control used to display a daily Dilbert comic on a site. The control is
extremely simple to build and it serviced through a public web service made
available by Chandu Thota on eSynaps.com. Once built, the control can be dropped
on any web page and it will retrieve a new comic from the Scott Adams Dilbert
archives for display each day.
The web service returns either a path to the image file
or an encoded copy of the image. This example just uses the file path option to
populate the image upon initialization.
Figure 1: Daily Dilbert Custom Control in Use
The files included with this project include a web
control library project and a demonstration web site. In order to get started,
open the included zip file and install the two projects onto your file system.
Open IIS and create a virtual directory for the web application. Open the
solution into Visual 2005 and make any changes necessary to bring both projects
into the solution. Once properly configured, your solution explorer should show
these projects, references, and files:
Figure 2: Solution Explorer with Web App and Control Library
In examining the solution, note that the "Dilbert"
control library contains only a single control and that control is called
"DilbertControl". This project also includes a web reference that points to the
this public site supplies the web service used to capture the Dilbert archive
cartoon displayed by the control.
The web application contains only a single web page (default.aspx)
and includes a reference to the "Dilbert" DLL.
The web application serves as a container used to test the custom control. The
page itself contains no addition code and the only other element on the page
displays the text "The Daily Dilbert".
The Code: Dilbert Control
The "Dilbert" custom control is constructed to retrieve
the information from the web service upon initialization; in the information
retrieved is a path to the URL of the cartoon image. Upon initialization, the
URL is retrieved and when the control is rendered, an image is created and
added; its imageURL property is set to point to the URL captured from the web
In examining the code, note that, only the default imports are included in the
project. The class itself inherits from the WebControl class.
Public Class DilbertControl
Following the class declaration, two member variables
are declared; one for the web service and one string variable used to contain
the path to the cartoon image file.
Next up is the initialization handler:
Private Sub DilbertControl_Init(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles Me.Init
mDilbert = New
control initialization handler creates a new instance of the "mDilbert" member
variable and then sets the "mDilbertPath" string variable to contain the image
URL returned by calling the services "DailyDilbertImagePath" web method.
The last detail to tend to is the rendering; here
RenderContents is overridden and, with a try - catch block, a div is started and
then an image with the image URL property set to point to string variable
containing the URL captured from the service, the image is then rendered and the
div is closed. If an error occurs, the HtmlTextWriter will just print out
"Dilbert is on vacation" in lieu of the cartoon.
Protected Overrides Sub
img.ImageUrl = mDilbertPath
is on vacation.")
project was just for fun although it does show a very basic example of an easy
approach to building a custom control that interacts with a web service.