Creating Dynamic ASP.NET Server Controls Using XML - Generation of Server Controls From Text
(Page 2 of 5 )
When Microsoft created ASP.NET, they wanted to allow classic ASP developers to continue using familiar development methods. One of these methods was save and run, where a developer would need only to save a page to apply code changes. This method relied on ASP pages being interpreted, which meant that no compilation step was needed before a page was ready to test and use. However, the .NET platform uses Microsoft Intermediate Language (MIL), which is generated in a compilation stage. To support the save and run development process, ASP.NET utilizes the .NET compiler in a dynamic fashion. This allows the ASP.NET system to recompile a page whenever it detects a change to that page's code. You may have noticed this step when you load an ASP.NET page, there is a brief delay the first time page is loaded. During this time, ASP.NET compiles the code contained in the page, if needed, and the .NET runtime converts the IL into platform-specific machine code.
It is ASP.NET's ability to dynamically compile code that can be used for a system that leverages XML and XSLTs. The general idea is to use the XSLT to create valid ASP.NET code for server controls, then, using built-in methods, parse this code into server controls, and insert those controls into the page structure. Because of the nature of the ASP.NET page lifecycle, the server controls created using this method have the same abilities as those defined when you design a page in Visual Studio.NET.
Details of the Process To use XML and an XSLT to generate and use ASP.NET code, you need to:
- Define a data source (XML document)
- Create an XSLT to transform XML into ASP.NET code
- Instantiate server controls defined by the generated ASP.NET code at runtime
- Insert the instantiated controls into the page's control collection
- Handle postback events from server controls
The XML document to be used as the data source can be any XML document you want. For example, it could be a user generated or supplied document, the result of a SQL Server XML query, an XML schema, or the result of a SOAP method call. The key is the XSLT that generates the ASP.NET code from the XML data. The ASP.NET code returned by the transformation is then instantiated by the Page.ParseControl method, which is in the System.Web.UI namespace. Note that although the method name suggests instantiation of a single control, this method will instantiate multiple controls, and return them as a control collection. The parsed controls can then be added to the page's control collection, either directly, or as the child of another control on the page.
This process should occur in the page's init event handler, so that the server controls are accessible during the rest of the page's lifetime. In addition, if the controls are added in this event handler, the ASP.NET runtime handles all state management and raising of events, such as button clicks and text changes. This allows you to define server side event handlers and reference the dynamically generated server controls in a standard way.
Dynamically added server controls do suffer from one disadvantage. To reference the controls from code written in the code behind file, you need to get a reference to the control. This can be done with the Page.FindControl method, which returns a reference to the control in the current naming container with the given name. This method uses late-binding, which will decrease performance, so you might want to keep a reference to the returned controls when you instantiate them, perhaps using a HashTable, with the control's name as the key, and reference to the control as the value.
HTML can also be output from the XSLT. The HTML will be converted to Literal controls that can be added to the page. Using HTML elements allows you to format server controls and data without the overhead of full ASP.NET formatting server controls, such as tables and panels. Formatting can also be handled by style attributes, but this comes at the cost of losing support for some older or non-standard browsers that don't fully support style attributes.
With any technology, there are a number of details that can cause problems. Dynamically generating server controls from an XML document is no different. These details are illustrated in the example of this technique.
Next: Custom Survey System Example >>
More ASP.NET Articles
More By Wrox Team