Click here to Skip to main content
13,296,407 members (47,585 online)
Click here to Skip to main content
Add your own
alternative version


136 bookmarked
Posted 15 Oct 2004

Generate dynamic ASP.NET pages by using XML and XSLT

, 15 Oct 2004
Rate this:
Please Sign up or sign in to vote.
Generates dynamic forms by transforming XML into XSL file.


Sometimes you need to create dynamic pages. You may allow users to build their own applications by selecting the fields they need. In this article, I will show how to build dynamic applications by using ASP.NET and XML. In this example, I use XML for meta-programming or generative programming. I use XML with an XSLT to generate ASP.NET PRE, utilizing intrinsic ASP.NET parsing methods with the XSLT output. Steps that we need to do:

  • Create XML file (schema of our page).
  • Create an XSLT style to transform XML into ASP.NET PRE.
  • Transform XML, and create server controls defined by the generated ASP.NET PRE at runtime.
  • Insert the instantiated controls into the page's control collection.
  • Handle postback events from server controls.

First, what we need to come up with is a good structure of our page in a XML file. A basic page structure is defined below. The example structure consists of field elements, with attributes, properties and listitems.

    <PAGE title="Page Title" id="page_1">
    <!-- create regular TextBox -->
    <FIELD type="TextBox" label="Last Name:" required="true">
            <PROPERTY name="ID">LAST_NAME</PROPERTY>

    <!-- create multiline TextBox (textarea) -->
        <FIELD type="TextBox" label="Description:">
            <PROPERTY name="TextMode">MultiLine</PROPERTY>
            <PROPERTY name="Cols">30</PROPERTY>
            <PROPERTY name="Rows">2</PROPERTY>

    <!-- regular textbox prefilled with text -->
    <FIELD type="TextBox" label="Prefilled Contrl:">
            <PROPERTY name="Text">Some Text</PROPERTY>

        <!-- regular text box which must filled 
                                 and allowed Date datata type -->
        <FIELD type="TextBox" label="Start Date:" 

                      required="true" validation="Date">

    <!-- create select box -->
        <FIELD type="DropDownList" label="Title:">
            <PROPERTY name="ID">TITLE</PROPERTY>
            <LISTITEM value="">Select One</LISTITEM>
            <LISTITEM value="1">Architector</LISTITEM>
            <LISTITEM value="2">Sr. Developer</LISTITEM>
            <LISTITEM value="3">Programmer</LISTITEM>
            <LISTITEM value="4">Web Designer</LISTITEM>

    <!-- radio buttons list -->    
        <FIELD type="RadioButtonList" label="Are you US citizen?">
            <PROPERTY name="RepeatColumns">1</PROPERTY>
            <PROPERTY name="RepeatDirection">Vertical</PROPERTY>
            <PROPERTY name="RepeatLayout">Table</PROPERTY>
            <PROPERTY name="TextAlign">Right</PROPERTY>
            <LISTITEM value="1">Yes</LISTITEM>
            <LISTITEM value="0">No</LISTITEM>

    <!-- check box list -->
        <FIELD type="CheckBoxList" label="Languages:">
            <PROPERTY name="RepeatColumns">1</PROPERTY>
            <PROPERTY name="RepeatDirection">Vertical</PROPERTY>
            <PROPERTY name="RepeatLayout">Table</PROPERTY>
            <PROPERTY name="TextAlign">Right</PROPERTY>
            <LISTITEM value="C#">C#</LISTITEM>
            <LISTITEM value="Java">Java</LISTITEM>
            <LISTITEM value="VB">Visual Basic</LISTITEM>

    <!-- here is a hyperlink (a) -->
        <FIELD type="HyperLink">
            <PROPERTY name="ID">LINK</PROPERTY>
            <PROPERTY name="NavigateUrl">javascript:void(alert('Hello ooo'));
            <PROPERTY name="Text">Say Hello</PROPERTY>

    <FIELD type="html" src="file path"/>

    <!-- you can place here any other asp controls -->



Next step is to create XSLT style to transform our XML schema The XSLT iterates through each field, first outputting a label for the field as plain text. The stylesheet also checks if the field is required, and adds a RequiredFieldValidator if needed. The stylesheet then creates a Web Control (it could be any valid web control such as TextBox, RadioButtonList, DropDownList, etc.). ListItems are created for each of the ListControls.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 

        xmlns:xsl="" xmlns:asp="remove">
  <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes">
  <xsl:template match="/">

   <!-- page id parameter if we have more than 1 page in our XML file -->
   <xsl:param name="pageid"/>

     <!-- start form page -->
     <table cellpadding="0" cellspacing="5">

       <!-- set title of the current page -->
        <td colspan="3" align="center" style="font-size:25px">
         <xsl:value-of select="FORM/PAGES/PAGE[@id=$pageid]/@title" />
       <tr><td colspan="3" style="height:20px"></td></tr>

       <!-- iterate through page fields -->
       <xsl:for-each select="FORM/PAGES/PAGE[@id=$pageid]/FIELDS/FIELD">

       <!-- create row -->
       <xsl:element name="tr">
        <xsl:attribute name="id">
          TR_<xsl:value-of select="PROPERTIES/PROPERTY[@name='ID']"></xsl:value-of>

        <!-- hide the row -->
        <xsl:if test="@display='none'">
         <xsl:attribute name="style">display:none;</xsl:attribute>


         <!-- this is the way to place "include" files into your PRE -->
         <xsl:when test="@type='HTML'">
          <td colspan="3">
            <!-- #include file="<xsl:value-of select="@src"></xsl:value-of>" -->

         <!-- other controls -->

         <!-- field label column -->
         <td valign="top">
          <xsl:value-of select="@label" />

         <!-- field column -->

          <!-- field element -->
          <xsl:element name="asp:{@type}">
           <xsl:attribute name="runat">server</xsl:attribute>
           <xsl:for-each select="./PROPERTIES/PROPERTY">
             <xsl:attribute name="{@name}">
               <xsl:value-of select="current()"></xsl:value-of>
           <xsl:for-each select="./LISTITEMS/LISTITEM">
            <asp:ListItem value="{@value}">
              <xsl:value-of select="current()"></xsl:value-of>

         <!-- validation message column -->
          <xsl:if test="@required='true'">
           <asp:RequiredFieldValidator ErrorMessage="Required" runat="server" 

                 ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}" />

          <xsl:if test="@validation='Date'">
           <asp:CompareValidator ErrorMessage="Dates Only" runat="server" 

                Operator="DataTypeCheck" Type="Date" 

                ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}" />

          <xsl:if test="@validation='Number'"> 
           <asp:CompareValidator ErrorMessage="Numbers Only" runat="server" 

               Operator="DataTypeCheck" Type="Integer" 

               ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}" />

          <xsl:if test="@validation='Currency'">
           <asp:CompareValidator ErrorMessage="Currency Only" runat="server" 

               Operator="DataTypeCheck" Type="Currency" 

               ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}" />



Our XSLT file has a prefix (xmlns:asp="remove") defined for the XSL namespace. This is used to generate plain HTML. Now we are ready to write our Page transformer.

private readonly string XslFile = @"...\default.xslt";
private readonly string XmlFile = @"...\default.config";

private void Page_Load(object sender, System.EventArgs e)
    string pageId = "page_1";

    if (!Page.IsPostBack)

         * Transform aspx page with fields from xml file
         * Get transform result as a string
         * Parse controls into a parent control holder

        XmlDocument xdoc = new XmlDocument();

        // load xslt to do transformation
        XslTransform xsl = new XslTransform();

        // load xslt arguments to load specific page from xml file
        // this can be used if you have multiple pages
        // in your xml file and you loading them one at a time
        XsltArgumentList xslarg = new XsltArgumentList();
        xslarg.AddParam("pageid", "", pageId);

        // get transformed results
        StringWriter sw = new StringWriter();
        xsl.Transform(xdoc, xslarg, sw);
        string result = sw.ToString().Replace("xmlns:asp=\"remove\"",
        // free up the memory of objects that are not used anymore

        // parse the controls and add it to the page
        Control ctrl = Page.ParseControl(result);

private void BtnSave_Click(object sender, System.EventArgs e)
    // save data into a data container
    // ...
    Response.Write (Request.Form["LAST_NAME"]);


In this article, we separate data from content to make a cleaner design and for better maintainability. When XML is combined with XSLT, ASP.NET server controls become even more powerful. This opens up numerous possibilities for dynamic and robust systems.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I am Microsoft Certified in .NET
I have over seven-year experience with client-server application development (full project lifecycle) on Windows platforms.
I've focused on creating middle tier, web application development, GUI development, custom data management software, and database programming.

You may also be interested in...


Comments and Discussions

QuestionProblem with the output of this project Pin
Will_FLBK29-Jun-08 23:40
memberWill_FLBK29-Jun-08 23:40 
GeneralNeeed working version for 2.0 Pin
krishnakonline18-Jun-08 4:45
memberkrishnakonline18-Jun-08 4:45 
GeneralHandling Events & Displaying data from database Pin
Anne_mathew12-Nov-07 22:06
memberAnne_mathew12-Nov-07 22:06 
QuestionEvent Handling Pin
pawan venugopal29-Aug-07 0:44
memberpawan venugopal29-Aug-07 0:44 
GeneralHey all, I figured how to get events to fire Pin
thisisavalidusername25-Jul-07 5:33
memberthisisavalidusername25-Jul-07 5:33 
AnswerRe: Hey all, I figured how to get events to fire Pin
Kr0d24-Sep-08 17:48
memberKr0d24-Sep-08 17:48 
GeneralRe: Hey all, I figured how to get events to fire Pin
shijitha7-Sep-10 2:16
membershijitha7-Sep-10 2:16 
QuestionHandling Events Pin
n8shadow13-Jul-07 8:06
membern8shadow13-Jul-07 8:06 
Generalnot able to run.. Pin
vijaysaxena2-Jul-07 1:49
membervijaysaxena2-Jul-07 1:49 
GeneralThe XSLT code has little error. Pin
V'ru29-Apr-07 19:26
memberV'ru29-Apr-07 19:26 
GeneralRe: The XSLT code has little error. Pin
Terence Golla4-Mar-10 7:39
memberTerence Golla4-Mar-10 7:39 
GeneralParameter value not getting Pin
V'ru26-Apr-07 3:55
memberV'ru26-Apr-07 3:55 
AnswerRe: Parameter value not getting Pin
KNet13-May-07 0:44
memberKNet13-May-07 0:44 
GeneralVB Code Pin
shafiqm223-Apr-07 6:54
membershafiqm223-Apr-07 6:54 
Hi Does anyone written an equivalent VB code, If yes please send it to Preferably in .Net2

GeneralGenerating Dynamic aspx pages Pin
Kherva5-Jan-07 8:45
memberKherva5-Jan-07 8:45 
QuestionAny Working copy for dotNet 2.0 ? Pin
SandeepNahta22-Nov-06 17:20
memberSandeepNahta22-Nov-06 17:20 
Generalsome files missing Pin
Vhakti9-Aug-06 11:13
memberVhakti9-Aug-06 11:13 
QuestionWorking 1.1 or 2.0 version? Pin
UCHetfield2-Aug-06 10:55
memberUCHetfield2-Aug-06 10:55 
AnswerRe: Working 1.1 or 2.0 version? Pin
cranshark24-Aug-06 10:53
membercranshark24-Aug-06 10:53 
GeneralRe: Working 1.1 or 2.0 version? Pin
rajsubram4-Sep-06 20:05
memberrajsubram4-Sep-06 20:05 
GeneralRe: Working 1.1 or 2.0 version? Pin
JAIME01814-Oct-07 17:54
memberJAIME01814-Oct-07 17:54 
GeneralRe: Working 1.1 or 2.0 version? Pin
Durga phani kanth9-Mar-08 21:47
memberDurga phani kanth9-Mar-08 21:47 
GeneralRe: Working 1.1 or 2.0 version? Pin
Anil_Tech24-Apr-08 13:25
memberAnil_Tech24-Apr-08 13:25 
General.Net 2.0 changes to transform class Pin
Backler27-Jul-06 14:41
memberBackler27-Jul-06 14:41 
GeneralApplication is not working Pin
vijay_mishra5-Jun-06 21:09
membervijay_mishra5-Jun-06 21:09 
GeneralRe: Application is not working Pin
JC@Rio12-Jun-06 10:22
memberJC@Rio12-Jun-06 10:22 
GeneralRe: Application is not working Pin
JC@Rio12-Jun-06 17:06
memberJC@Rio12-Jun-06 17:06 
GeneralRe: Application is not working Pin
Durga phani kanth9-Mar-08 21:55
memberDurga phani kanth9-Mar-08 21:55 
GeneralRe: Application is not working Pin
Member 371107619-Jun-08 5:41
memberMember 371107619-Jun-08 5:41 
GeneralRe: Application is not working Pin
Member 371107619-Jun-08 5:43
memberMember 371107619-Jun-08 5:43 
GeneralGetting Values from Controls Pin
stevelabun6-Apr-06 9:34
memberstevelabun6-Apr-06 9:34 
GeneralGood try, but... Pin
Rong Chen12-Dec-05 16:12
memberRong Chen12-Dec-05 16:12 
GeneralRe: Good try, but... Pin
cranshark24-Aug-06 11:18
membercranshark24-Aug-06 11:18 
GeneralRe: Good try, but... Pin
JC@Rio15-Nov-06 9:05
memberJC@Rio15-Nov-06 9:05 
Questionnamespace WTMI Util and Collections? Pin
JimWPerkins30-Aug-05 12:40
sussJimWPerkins30-Aug-05 12:40 
Generalexpect new release!!! Pin
HelloSnoopy23-Aug-05 16:41
memberHelloSnoopy23-Aug-05 16:41 
GeneralRe: expect new release!!! Pin
Anil_Tech7-Sep-05 13:40
memberAnil_Tech7-Sep-05 13:40 
GeneralRe: expect new release!!! Pin
bigals11-Jan-06 12:10
memberbigals11-Jan-06 12:10 
QuestionHow to validate,save checkboxlist values selected by user Pin
anil35721-Jul-05 22:33
memberanil35721-Jul-05 22:33 
GeneralExecuting methods from code-behind Pin
bruh_man12-Jul-05 12:40
memberbruh_man12-Jul-05 12:40 
GeneralDo you have an example in VB not in C# Pin
Anonymous30-Jun-05 10:44
sussAnonymous30-Jun-05 10:44 
Generalxml with namespaces : bind to datagrid Pin
chandler sue31-May-05 23:51
memberchandler sue31-May-05 23:51 
Generalbinding datagrid with xml Pin
chandler sue31-May-05 23:49
memberchandler sue31-May-05 23:49 
Questionhow can I save the control state Pin
HelloSnoopy31-May-05 17:42
memberHelloSnoopy31-May-05 17:42 
AnswerRe: how can I save the control state Pin
Christian Graus31-May-05 18:37
memberChristian Graus31-May-05 18:37 
GeneralRe: how can I save the control state Pin
HelloSnoopy31-May-05 20:10
memberHelloSnoopy31-May-05 20:10 
GeneralRe: how can I save the control state Pin
Christian Graus31-May-05 22:39
memberChristian Graus31-May-05 22:39 
AnswerRe: how can I save the control state Pin
chandler sue31-May-05 23:54
memberchandler sue31-May-05 23:54 
AnswerRe: how can I save the control state Pin
HelloSnoopy1-Jun-05 21:22
memberHelloSnoopy1-Jun-05 21:22 
GeneralDeveloping a dynamic website using XML, XSL, C# and SQL Server Pin
T7P11-Apr-05 23:01
memberT7P11-Apr-05 23:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 15 Oct 2004
Article Copyright 2004 by rafael_p
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid