Hosting a Windows Control in a Web Form
- Offer more functionality than just ASP.NET. For example complex treeviews, circular drawing, and user interaction are easier to build in Windows forms than ASP.NET.
- Offer better performance because complex functionality doesn’t require postbacks. The client downloads the control once, and then can interact with it without reposting to the server.
- Save time because the control may already be built, and be sufficiently complicated that there’s not time to rebuild it on a busy project.
- Act as an intermediate step to migrate a Windows application to or from ASP.NET
First Things First: Creating the Windows Control
- A Windows Control Library that contains the control itself,
- A Windows Application to test that control, and
- An ASP.NET Web Application, showing how to host a Windows Control in a Web page.
All of these projects are available for download at the end of this article. To get started, first create a blank solution named HostWinControlInAspx . Create this in your C:inetpubwwwroot because it will make it cleaner when you integrate it with the ASP.NET project later. Now add to the solution a Windows Control Library (NOT a Class Library). Name this new project WinControls . For starters we’ll create just a single User Control named WinTreeview.cs . Drag a GroupBox onto the control, and then drag a Treeview into the GroupBox. Use the GroupBox’s text property for a title, and encapsulate this like so (in C#):
When finished, the Windows User Control should look something like the image shown below.
Let’s test this control by adding a Windows Application project (named WinTestControls ) to the solution. Note that it is much easier to test and debug a Windows User Control in a Windows App than in an ASP.NET App. From the ToolBox’s “My User Controls” section, drag your User Control onto the default Form1. Look in the User Control’s Properties window, and set Title to My Title . Set the Windows App as the startup project and run it to confirm that the User Control works correctly.
Lastly, let’s create the ASP.NET Web application that actually hosts the Windows Control. Add a new ASP.NET Web application project to the solution and name it WebGUI . In IIS, the application needs to be configured for “Execute Permissions” to be “Scripts Only”, which Visual Studio does by default.
Copy the physical DLL (as opposed to adding a Reference) from the Windows Control library into the root folder for the Web app. Simply adding a reference will not allow you to reference the Windows Control. Likewise if you update your Windows Control, rebuilding the solution won’t get the changes for the ASP.NET project – you need to recopy the DLL instead.
Although you could host the control in an HTML file, let’s create a WebForm named SetStatic.aspx , and add the following HTML in the body:
If you have added Java applets of ActiveX controls to a Web page before, you’ll note that adding a Windows Control to an ASP.NET Web page uses the same HTML markup – an “;
this.Literal1.Text = strObject;
The Title PARAM ‘s value is dynamically set based on the value from the Title TextBox. You can use this technique to create instances of the Windows Control at run time, as well as set its properties. Using this dynamic approach, you’re page should look something like:
Hosting a Windows Control in a Web Form Introduction Offer more functionality than just ASP.NET. For example complex treeviews, circular drawing, and user interaction are easier to build
SOURCE: Asp host http://www.4guysfromrolla.com/articles/052604-1.aspx Asp host