Creating Dynamic ASP.NET Server Controls Using XML (Page 1 of 5 )
ASP.NET has made website development easier with its array of server controls that expose sophisticated behaviors, such as data binding and validation, along with a rich programming model that can directly interact with these server controls. XML and XSLT technology has also made website development better by allowing the separation of content and presentation.
A common way of using XML in ASP websites is to format dynamic information, such as query results, into a destination format (HTML or PDF). However, if you wanted to allow the user to interact with the returned information via a form, you had to use a mix of traditional ASP techniques, such as iterating over the Request.Form collection, and outputting HTML form elements. This approach works with .NET, but you lose the powerful features of ASP.NET server controls, such as data binding, state management, input validation, and the rich event model.
If you have used strongly typed datasets in .NET, you have seen the power of combining XML with the .NET platform. A strongly typed dataset consists of a number of classes whose code is generated by an XML document that defines what the code should do. The xsd.exe tool then generates the actual code based upon the requirements as defined in the XML document. XML then becomes a tool for meta-programming (or generative programming), which is the process of using code to generate code. The goal here is the same - to show how to use XML with an XSLT to generate ASP.NET code, specifically server controls. Utilizing intrinsic ASP.NET parsing methods with the XSLT output, you will have another meta-programming tool to make website development faster, easier, and better.
To illustrate this technique, we will create an example application - a custom survey system, where an XML document defines the structure of a web survey. The XSLT defines the layout and behavior of the web page, as well as postback behavior, allowing for a completely dynamic system, which, by using ASP.NET cross browser support, will work on all browsers. This dynamic nature could be used to allow users or content maintainers to define surveys by creating new XML documents, develop branching surveys based on a previous input, and more, all without the need to manually write any new code. All these feature extensions can be accomplished by editing the XSLT that generates the ASP.NET code.
By allowing non-programmers to create an XML document that will generate code, you can reduce the load on ASP.NET developers, but still leverage all of ASP.NET's power. In essence, you can have a system that allows users to specify what they want, and not need to know how to make it happen. This particular example will use a simple results storage system, as well as omitting some processes that would be needed in a robust system, as the purpose is not to present a complete system, but to illustrate a meta-programming technique.
Requirements The code and techniques described here require ASP.NET, Windows 2000 or XP, along with IIS. The code is written in C#, and assumes knowledge of ASP.NET, XML and XSLT.