Wednesday 12 October 2011

Generate dynamic ASP.NET pages by using XML and XSLT


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:
  • Create XML file (schema of our page).
  • Create an XSLT style to transform XML into ASP.NET PRE.
  • Transform XML, and create server controls defined by the generated ASP.NET PRE at runtime.
  • Insert the instantiated controls into the page's control collection.
  • Handle postback events from server controls.
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 aRequiredFieldValidator if needed. The stylesheet then creates a Web Control (it could be any valid web control such as TextBoxRadioButtonListDropDownList, etc.). ListItems are created for each of the ListControls.



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 fordynamic and robust systems.

No comments:

Post a Comment