Maintain State With Dynamic Controls
Build a process controller to simplify multistep input.
by Garry McGlennon
April 2003 Issue
Technology Toolbox: VB.NET, ASP.NET
ASP.NET gives you the opportunity to create reusable custom controls that maintain state, and drag and drop them onto Web pages. This is a huge advantage in designing pages, because it lets you bring the Win32 programming model to the Web. ASP.NET also allows you to create the same controls dynamically at run time. Microsoft has done a good job, in general, of enabling you to create dynamic controls; however, things don't always work as planned. I'll discuss the issues you face when you create dynamic controls, and I'll show you how to address them. I'll also demonstrate the use of dynamic controls by showing you an example of a process controller; you can use the same technique for any multistep process.
You need only this line of code to create a control dynamically at run time:
Me.Panel1.Controls.Add(Page. _
LoadControl("~/MyControl.ascx")
You must use the fully qualified path of the control you're loading; otherwise, you might get a "file not found" error. A good tip is to use the tilde (~) at the beginning, because it represents the root of the Web application when you use it with server-side controls.
You can add controls to any container control that supports the Controls collection, such as a Panel or Placeholder control. The DynamicControlHost.asx control you'll build later uses the Panel control (see Figure A). There's no visual content, because you add the content dynamically each time the page loads.
Now I'll show you how to create a process controller that demonstrates the use of dynamic controls. It handles the series of steps or screens required to register someone on a Web site. You can use the finished control anywhere within the site by dragging and dropping it on a page. A traditional wizard-style interface that encapsulates the steps is the process controller's core (see Figure B).
Back to top
|