ASP.NET
  Home arrow ASP.NET arrow Page 3 - Creating Dynamic ASP.NET Server Controls U...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Creating Dynamic ASP.NET Server Controls Using XML
By: Wrox Team
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 116
    2002-12-12

    Table of Contents:
  • Creating Dynamic ASP.NET Server Controls Using XML
  • Generation of Server Controls From Text
  • Custom Survey System Example
  • XML Namespaces
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By Wrox Team


     

    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







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway