Creating Dynamic ASP.NET Server Controls Using XML - Custom Survey System Example
(Page 3 of 5 )
The following example is a custom survey system, in which an XML document defines the structure of the survey and the XSLT transforms the survey into server controls. Each step detailed above is illustrated with code examples.
Data Source A basic survey structure is defined below. In a more robust solution, an XML schema would be written to ensure that all surveys follow the correct format. The example structure consists of a survey element, with a name attribute and one child element for each question. Each question element contains a type attribute, a name attribute, and an optional required attribute. If the question is a multiple-choice question, each choice is a child element of the question.
<survey name="Example Survey">
<question type="text" name="Title" required="yes"/>
<question type="text" name="Industry"/>
<question type="radio" name="Education">
<choice>High school</choice>
<choice>Some college</choice>
<choice>College</choice>
</question>
</survey>
As you can see, in this example there are three questions, two text questions and one multiple-choice question. Note that the first question is required, which means that the server controls should prevent submission of the form if that question is left blank, as can be seen in Figure 2. In a more complete application, validation controls with regular expressions could be used, or any other technique available in ASP.NET.
Create XSLT The XSLT is the key to the process as it generates the ASP.NET code that will create the server controls.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:asp="remove">
<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-
declaration="yes"/>
<xsl:template match="/">
<table>
<xsl:for-each select="//question">
<tr>
<td valign="top"><xsl:value-of select="@name" />
<xsl:if test="@required = 'yes'">
<asp:RequiredFieldValidator ErrorMessage="Required
Field" runat="server" ControlToValidate="{@name}" />
</xsl:if>
</td>
<td>
<xsl:if test="@type='text'">
<asp:TextBox id="{@name}" runat="server" />
</xsl:if>
<xsl:if test="@type='radio'">
<asp:RadioButtonList id="{@name}" runat="server">
<xsl:for-each select="choice">
<asp:ListItem Value="{@value}">
<xsl:value-of select="@value"/>
</asp:ListItem>
</xsl:for-each>
</asp:RadioButtonList>
</xsl:if>
</td></tr>
</xsl:for-each>
</table>
<asp:button id="submit" runat="server" Text="Submit" />
</xsl:template>
</xsl:stylesheet> The XSLT iterates through each question, first outputting a label for the question as plain text. The stylesheet also checks if the field is required, and adds a RequiredFieldValidator if needed. The stylesheet then creates a TextBox for the text questions and a RadioButtonList for the radio questions. ListItems are created for each of the choices in the radio button questions. Surrounding the server control declarations are standard HTML table elements, to format the questions. Finally, a submission button is added to the end of the form.
Next: XML Namespaces >>
More ASP.NET Articles
More By Wrox Team