Get a Rich UI With WinForms

Use a DataReader to enumerate multiple sets of query results.

Jeff Prosise

October 30, 2009

6 Min Read
ITPro Today logo

Ask the PRO

LANGUAGES: C#

ASP.NET VERSIONS: 1.0 | 1.1

 

Get a Rich UI With WinForms

Use a DataReader to enumerate multiple sets of queryresults.

 

By Jeff Prosise

 

Q: I'm interested in using Windows Forms controls tobuild rich user interfaces in ASP.NET Web pages. I know how to deploy thecontrols and access their properties and methods from client-side script. Is italso possible to process a Windows Forms control's events in the browser?

 

A: It's no secret that one way to overcome the limitationsof HTML and endow browser-based clients with rich user interfaces is to hostWindows Forms controls in Web pages. As an example, here's a derived classnamed WebSlider that encapsulates a Windows Forms TrackBar control:

 

namespace Wintellect

{

    public class WebSlider:

        System.Windows.Forms.TrackBar {}

}

 

If this source-code file is compiled into a DLL namedWebSlider.dll and deployed on a Web server, this tag declares an instance ofit, causing a vertical TrackBar control to appear on the page:

 

 

The first time the page is accessed, Internet Explorerdownloads the DLL and, with the .NET Framework's help, caches it on the client.The two chief requirements are that the client must be running InternetExplorer 5.01 or higher and must have the .NET Framework installed.

 

Accessing the control's properties and methods usingclient-side script is simplicity itself. If the form containing the control isnamed MyForm, this JavaScript statement moves the TrackBar thumb to position 5by setting the control's Value property:

 

MyForm.Slider.Value = 5;

 

Writing client-side script that processes events fired bythe control, however, is less straightforward. First, you must define aninterface that encapsulates the events you wish to expose to the browser, andyou must instruct the .NET Framework to expose the interface's members througha COM IDispatch interface. Then, you must associate this interface with thecontrol.

 

Figure 1 contains the source code for a class derived bySystem.Windows.Forms.TrackBar that exposes the Scroll events fired in responseto thumb movements to unmanaged code. The IWebSliderEvents interface definesthe event as a method and assigns it a dispatch ID. (In COM, all methodsexposed through an IDispatch interface require unique integer dispatch IDs.)Note that the method signature exactly matches that of the Scroll event definedin the base class. The [InterfaceType] attribute tells the .NET Framework toexpose the IWebSliderEvents interface to unmanaged code as an IDispatchinterface. The [ComSourceInterfaces] attribute adorning the class definitionlets the framework know that WebSlider should support IWebSliderEvents as asource interface, which is COM speak for an interface used to source (fire) events.

 

using System;

using System.Runtime.InteropServices;

 

namespace Wintellect

{

   [ComSourceInterfaces(typeof (IWebSliderEvents))]

  public class WebSlider :System.Windows.Forms.TrackBar {}

 

   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

   public interface IWebSliderEvents

  {

     [DispId (1)] voidScroll (Object sender, EventArgs e);

  }

}

Figure 1. This WindowsForms TrackBar control derivative exposes Scroll events to unmanaged code.

 

Figure 2 lists an .aspx file you can use to test the WebSlidercontrol. The

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like