Home arrow ASP.NET arrow Page 3 - Creating Dynamic ASP.NET Server Controls Using XML
ASP.NET

Creating Dynamic ASP.NET Server Controls Using XML


A common way of using XML in ASP websites is to format dynamic information. In this article Eric shows us how to create a survey application using ASP.Net and XML.

Author Info:
By: Wrox Team
Rating: 5 stars5 stars5 stars5 stars5 stars / 205
December 12, 2002
TABLE OF CONTENTS:
  1. · Creating Dynamic ASP.NET Server Controls Using XML
  2. · Generation of Server Controls From Text
  3. · Custom Survey System Example
  4. · XML Namespaces
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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>


The Sample application

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.

The Title field is compulsory

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.
blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials