tags:

views:

966

answers:

3

I wish to use xml and xsl to generate controls on an asp.net page.

I currently have a asp.net content page that contains a xml control. When the page is loaded, an xml file is loaded and the required element is extracted and set as the xml control's DocumentContent and the xml control's TransformSource is set to the appropriate xsl file.

In the xsl file, I wish to use templates to create asp.net controls depending upon the contents of the xml. Is there an easy way of doing this with the xml control on the content page?

A: 

The generated output of transform is not parsed to be added to the page as set of controls. Rather the generated output is sent to the response. Therefore you cannot apply a transform to an XML control that will generate new controls.

There may be a way to create the transform result and invoke some parsing that can be applied to the page but that would not meet your 'easy' requirement.

AnthonyWJones
A: 

I don't know of any easy way to do it, but there are a few systems out there which generate the aspnet markup on the fly then send it to the aspnet runtime from a memory stream (or similar).

I think umbraco uses a system something like that, but the initial builder mechanism is probably a little more low level than an aspnet control. It might be achievable using an httpmodule or something else that gets hit earlier in the request pipeline.

seanb
+1  A: 

Register a namespace in your XSL so that you can put .NET server-side tags into it. Run your XSL transform against your XML. Run the result of your transform through Page.ParseControls(). This will give you a nice control hierarchy. Add the output of that function to your Controls collection (or the controls collection of your placeholder) and you're good to go.

There are LOTS of caveats to using dynamic controls in .NET, so beware.