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.
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.
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.