Smart Clients: Windows Forms Flexibility with Web Application Ease
Yesterday, while working on some Web clients, I caught myself thinking if only it were a Windows Form application... What I was doing would've been easier to do in a Windows Forms application (which probably applies to whatever you happen to be doing as well). However, because it was a Web application, I knew I had reached a small crossroad. Although the natural next step might've been to use a Windows Forms control in a Web page (which in the pre-.NET era was as simple as embedding ActiveX controls), something that technically can be done in ASP.NET, dynamic use of the Windows Forms control in a Web page is difficult and laborious.
In this article, you will learn the basics of embedding a Windows Forms control in ASP.NET and gain an understanding of why this isn't the correct approach and why smart clients are. A smart client basically is a Windows Form application that dynamically updates client assemblies. This article introduces smart clients, providing you with enough information to explore them further and determine when you might need to employ them.
By the time you have finished reading, I hope you agree that Microsoft has an obligation to merge the two technologies, ASP.NET and Windows Forms, and completely hide whether one is programming for a network or not—it just makes good sense. From a user's perspective, it shouldn't matter anyway.
You may also draw the conclusion that smart clients indicate where technology is headed—no more script, no HTML, and no more code-behind, just GUIs and code like it was in the old days. Smart clients are a stepping stone on this journey; we aren't there yet.
Adding a Windows Forms Control to an ASP.NET Page
Suppose you have a Windows Forms control that is either difficult to or too expensive to reproduce for Web Forms. You can use the actual Windows Forms control in a Web Form (ASP.NET) application by:
- Adding the Windows Forms control to a Windows Control Library
- Copying the Windows Control Library to the Web Application's root folder
- Adding an <OBJECT> tag to the ASP for the page in which you want to include the Windows Forms Control Library
Building the Windows Control Library
A Windows Forms Control Library creates a .DLL project with references to the System.Windows.Forms assembly. This is an approximation to an ActiveX control library that you may be more familiar with from VB6.
Suppose, for argument's sake, you'd like to use the Windows Forms calendar control in a Web page. You can add a Windows Control Library to an existing project by selecting Add|New Project from the Solution Explorer context menu and picking the Windows Control Library applet from the Add New Project dialog (see Figure 1).
Figure 1: Add a Windows Control Library project to a Web Application.
For our purposes, it is sufficient to drag and drop a DateTimePicker control to the UserControl (defined as System.Windows.Forms.UserControl) added to the Windows Control Library project by the project template. I renamed the UserControl class DateTimeUserControl. (It doesn't matter what you call the UserControl namespace and class, but you do need to know this information later in the process.) When you are finished, your desktop might look something like Figure 2.
Figure 2: The Windows Forms UserControl with the DateTimePicker.
Embedding the Windows Control in a Web Page
The next step is to compile the Windows Control Library and copy the DLL assembly to the root folder of the Web Project (or Projects) that will embed this control. My ASP.NET project is named UseWinControl. As shown in Figure 3 (if you click Show All Files), the Windows Control Library will be positioned at a higher folder level than the bin folder.
Figure 3: Place the Windows Control Library in the ASP.NET application's root folder, as shown.
Adding a project reference to the Windows Control Library won't work because .NET will copy the output of the Windows Control Library to the bin folder, and you want it in the ASP.NET root folder.
The final step is to add an <OBJECT> tag to the Web Forms ASP. Add an <OBJECT> tag in each place and on each ASP.NET Web Form on which you want to use the control. The format of the <OBJECT> tag is shown in Listing 1.
Listing 1: The <OBJECT> tag for our Windows Control
<OBJECT id=DateTimeUserControl1 height=50 width=200 classid=http:EmbedWinControl.dll#EmbedWinControl.DateTimeUserControl VIEWASTEXT> <PARAM NAME="SelectedDateTime" VALUE="<%=DateTime.Now %>"> </OBJECT>
In the listing, I provide an ID for the control and its size. You'll need to provide a class ID, which can be a URN or GUID. I used a URN (or URL) that includes the HTTP moniker and the relative path of the DLL followed by a pound sign (#) and the namespace and class of the control. The designer uses VIEWASTEXT to figure out how to show the text. Next, I provide any parameter values using the PARAM tag and close the <OBJECT> tag with </OBJECT>. The parameter is a public property I added to the UserControl; it simply surfaces the DateTimePicker's Value property. (See surfacing constituent controls and properties in the VS.NET help for more information.) The result as coded is shown in Figure 4.
Figure 4: The DateTimePicker in a Web Form running in Internet Explorer.
Page 1 of 2