Create Rich User Interfaces by Injecting Windows Forms Controls
into Your ASP.NET Web Pages
By Steve C. Orr
Developers love Web applications because they are easier
to deploy than Windows applications. But developers love Windows applications
because functionally rich user interfaces can be created rapidly. Wouldn t it
be nice to have the best of both worlds? In this article I ll show you how you
can have your cake and eat it too by supercharging your ASP.NET Web pages with
Windows Forms controls.
Why Use Windows Forms Controls?
There are many examples of things that can be done easily
with Windows Forms controls that would be difficult or impossible with Web
controls alone. For example, there is no Web control that provides the rich
functionality of the Windows Forms ComboBox control. So what s to stop a
developer from using the ComboBox control on their Web forms? Not much, as will
be demonstrated shortly. The custom ComboBox control defined in this article
can even fetch its own data from a Web service.
How about interactive images that can constantly redraw
and keep themselves up do date? The Bar Graph custom Windows control detailed
later in this article will demonstrate a mesmerizing animated bar chart that
can redraw itself based on user input without having to call back to the
Browser and .NET Framework Detection
Every user will need two things to successfully consume Windows
Forms controls in a Web page: Internet Explorer and the .NET Framework. These
requirements shouldn t be hard to meet these days, but there will sometimes be
a minority of users who don t meet the requirements. Luckily, as a developer,
you can help them find their way. A few lines of code are all it takes to
verify the user has Internet Explorer and the .NET Framework installed; if they
don t, you can redirect them to an alternate page or guide them toward a
Dim msg As String
If Request.Browser.Browser.ToUpper().IndexOf("IE") >=
Request.Browser.ClrVersion.Major > 0 Then
Msg ="Your browser
is good and powerful."
msg ="Get the .NET
Framework from www.WindowsUpdate.com"
page requires Internet Explorer.")
The Request.Browser property contains information about
the user s Web browser. In this example, searching for the string IE confirms
the user is running Internet Explorer.
The Request.Browser.ClrVersion property contains detailed
version information for the most up-to-date version of the .NET Framework that s
installed on the user s machine. The Major property will be zero if the user
doesn t have any version of the .NET Framework installed. The GetClrVersions
method (not shown) will return an array of all the versions of the .NET Framework
that are installed on the user s machine (in case you need such detailed
information). In the vast majority of cases, that level of detail shouldn t be
necessary, but it s nice to know it s there.
To embed a custom Windows control in a Web page, you ll
need an HTML snippet such as this:
A special technique is used to overload the ClassID
attribute so all the necessary information can be specified to launch the .NET
component. The ClassID must start with http: , which must be followed by the
name of the DLL containing the .NET component. This DLL must reside in the Web
application s root directory. After the DLL name there must be a pound sign (#),
followed by the full name of the component (namespace included).
tags can be used to declaratively set
properties of the control. In the previous example, the BackColor property of
the control is set to yellow. Client-side script can also be used to call
properties and methods of the custom .NET Windows control, as will be demonstrated
later in the article.
The unsupported browser text will only appear in cases
where the message is true.
A Basic ComboBox
Using the technique just described, it s not possible to
refer to any components that reside in the GAC, and, therefore, it s impossible
to refer directly to a standard Windows control. It is possible to use standard
Windows controls in your Web pages, but they must be encapsulated within a
custom assembly that can be referred to with the precise HTML syntax previously
listed. It s not difficult.
For example, compile this simple class into a new Windows
Control Library using Visual Studio:
Public Class MyComboBox
Add the resulting control to a Web page using the earlier
HTML snippet syntax and you re good to go. Don t forget to place the control s
resulting DLL in the Web application s root directory, or else it won t be
downloaded to the user s computer.
An Enhanced ComboBox
A ComboBox isn t very useful unless you can get data into
and out of it. The above ComboBox control will be enhanced to retrieve its own
data from a Web service.
After setting a reference to a local Web service that
returns a DataSet, this improved ComboBox control calls the Web service back on
its home server and fills itself with the resulting data (see Figure 1).
Public Class ComboBox
Public Sub New()
'Fetch data from a web
service and fill the ComboBox
Dim ws As New
For Each dr As DataRow
Figure 1: Intended
to be hosted within a Web page, this customized Windows ComboBox control
retrieves its own data from a Web service.
This control inherits from the standard Windows Forms
ComboBox control, thereby getting all its functionality. It then extends the
standard functionality by calling a Web service from within the constructor. Finally,
it loops through the records and adds them to the Items collection of the
ComboBox. Figure 2 shows it in action.
Figure 2: Don t be discouraged by
the fact that there s no ComboBox Web control in ASP.NET. The Windows Forms
ComboBox control can be used in Web applications.
To retrieve the value the user selects in the ComboBox, an
the ComboBox value is placed into an HTML HiddenTextBox control just before the
page is submitted back to the server:
The bar graph shown in Figure 3 is another example of a
custom Windows Forms control that doesn t need to post back to the server. In
fact, there is no server-side code needed at all for this page, as you can see
by the plain HTML page listed in Figure 4.
Figure 3: It would be difficult if
not impossible to implement such a rich, animated bar graph as this with HTML
than Web controls.
Figure 4: Windows
controls can be embedded in plain HTML pages, with no server-side required. In
control that are hosted within the browser.