Creating Dynamic ASP.NET Server Controls Using XML

119308 Users read it.
by John Chapman (December 11, 2002)

This article was originally published on ASPToday (

One of the advantages of the XML/XSLT combination is the ability to separate content from presentation. A data source can return an XML document, then by using an XSLT, the data can be transformed into whatever HTML is needed, based on the data in the XML document.
The release of ASP.NET changes this picture somewhat.

ASP.NET provides server controls that have advanced functionality, such as state management, input validation, and data binding. In order to use this functionality, you need to create the controls on the server in a design time phase or at runtime from code.

The place of XML/XSLT technology in the ASP.NET model is not obvious, as the server controls generate their own HTML. The flexibility of XML/XLST can be combined with the power of ASP.NET server controls by using an XSLT to generate the server controls dynamically, thus leveraging the best of both worlds. This synergy is demonstrated by creating a survey application, in which the UI of the survey is based on an XML document defining the survey questions.

User Rating (326 votes)
33 reviews available | Read the reviews »
Author(s):Erick Thompson
User Level:Intermediate


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.


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.

User Reviews

Total of 33 reviews
Solution of FindControl problem faced by others
Written by Gaurav Verma on August 19, 2010

Simply replace the line below in Red with line in Green below it.. object ctrl = FindControl(controlName); object ctrl = survey.FindControl(controlName); Thats it.

Written by pat frmhll on July 26, 2009

why the fuck is it not possible to view the whole goddamn comments jesusmotherfuckingchrist that dipshit event not firing problem is driving me infuckingsannee

Written by Dean Smith on January 22, 2009

Can anybody post their solution for handling the button click.

I should have mentioned ...
Written by Greg Cymbala on February 20, 2008

I should have been more clear. In the 'MakeControls.xslt' file, change this line: <asp:button id='submit' runat='server' text='Submit' /> To something like this:<asp:button id='btnSubmit' runat='server' text='Submit' />

OMG, I just wasted hours!
Written by Greg Cymbala on February 20, 2008

OK, this code has one serious flaw that will likely cause many people who use it to scratch their heads for a long time.I downloaded the code (it's here) and tried adding some LinkButtons and DropDownLists with server-side events. I had no ...

can't see the download link
Written by Aung Ye'Htut on January 31, 2008

Hi All, I can't see the download link. Please help me..

RE: "Figured out how to get events to fire" and "FindControl not working"
Written by Curt Zarger on November 13, 2007

I've got this working except for the same event-firing problem mentioned in the last responses. My Button code is never hit at runtime. The last two responses indicate this has been solved, but I don't understand the answer relating to use of ...

FindControl not working
Written by Dan Enticknap on September 18, 2007

All is working well apart from the FindControl Method which returns Null each time. I've even tried using a RecursiveFindControl Method and that returns the same. Help, using c# v2. Dan

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency