Introduction

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:

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.

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.

<?xmlversion="1.0"encoding="UTF-8"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:asp="remove"><xsl:outputmethod="xml"indent="yes"encoding="utf-8"omit-xml-declaration="yes"></xsl:output><xsl:templatematch="/"><!-- page id parameter if we have more than 1 page in our XML file --><xsl:paramname="pageid"/><!-- start form page --><tablecellpadding="0"cellspacing="5"><!-- set title of the current page --><tr><tdcolspan="3"align="center"style="font-size:25px"><xsl:value-ofselect="FORM/PAGES/PAGE[@id=$pageid]/@title"/></td></tr><tr><tdcolspan="3"style="height:20px"></td></tr><!-- iterate through page fields --><xsl:for-eachselect="FORM/PAGES/PAGE[@id=$pageid]/FIELDS/FIELD"><!-- create row --><xsl:elementname="tr"><xsl:attributename="id">
TR_<xsl:value-ofselect="PROPERTIES/PROPERTY[@name='ID']"></xsl:value-of></xsl:attribute><!-- hide the row --><xsl:iftest="@display='none'"><xsl:attributename="style">display:none;</xsl:attribute></xsl:if><xsl:choose><!-- this is the way to place "include" files into your PRE --><xsl:whentest="@type='HTML'"><tdcolspan="3"><!-- #include file="<xsl:value-of select="@src"></xsl:value-of>" --></td></xsl:when><!-- other controls --><xsl:otherwise><!-- field label column --><tdvalign="top"><xsl:value-ofselect="@label"/></td><!-- field column --><td><!-- field element --><xsl:elementname="asp:{@type}"><xsl:attributename="runat">server</xsl:attribute><xsl:for-eachselect="./PROPERTIES/PROPERTY"><xsl:attributename="{@name}"><xsl:value-ofselect="current()"></xsl:value-of></xsl:attribute></xsl:for-each><xsl:for-eachselect="./LISTITEMS/LISTITEM"><asp:ListItemvalue="{@value}"><xsl:value-ofselect="current()"></xsl:value-of></asp:ListItem></xsl:for-each></xsl:element></td><!-- validation message column --><td><xsl:iftest="@required='true'"><asp:RequiredFieldValidatorErrorMessage="Required"runat="server"ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}"/></xsl:if><xsl:iftest="@validation='Date'"><asp:CompareValidatorErrorMessage="Dates Only"runat="server"Operator="DataTypeCheck"Type="Date"ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}"/></xsl:if><xsl:iftest="@validation='Number'"><asp:CompareValidatorErrorMessage="Numbers Only"runat="server"Operator="DataTypeCheck"Type="Integer"ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}"/></xsl:if><xsl:iftest="@validation='Currency'"><asp:CompareValidatorErrorMessage="Currency Only"runat="server"Operator="DataTypeCheck"Type="Currency"ControlToValidate="{PROPERTIES/PROPERTY[@name='ID']}"/></xsl:if></td></xsl:otherwise></xsl:choose></xsl:element></xsl:for-each></table></xsl:template></xsl:stylesheet>

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.

Conclusion

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.

License

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.

I am adding dynamic controls to the ASP.NET page by reading an xml and use xslt to create the controls. Now I am able to load the controls dynamically. My next step is to capture the data entered into these dynamically generated controls and save the data into the database. Could you please help!

I'm going to create a dynamic template with asp.net using c#.the template page is consist of three separate parts. first main menu which is located at the top bar.second sub menu which is located at the left side bar.and third part is right side of the page which is show the details.the process is: if you click on the any of the main menu the left side bar should refresh only not all the page and show all the sub-menu. then once you choose any of the sub-menu the right side should only refresh and show the details of the sub-menu.the main point is i don't want to refresh all page by each menu selection.could you please guide me for this template.appreciate your consideration.

You can see the form on your web page if you have bind this string to and cotrol on you page e.g Control tr = Page.ParseControl(result); this.thisDiv.Controls.Clear(); // thisDiv is the dive on you page with run at server this.thisDiv.Controls.Add(tr);