tags:

views:

920

answers:

4

hi! i'm generating controls dynamically on my asp.net page by xslt transformation from an xml file. i will need to reference these controls from code behind later. i would like to add these references to the list/hashtable/whatever during creation (in xslt file i suppose) so that i could reach them later and i have no idea how to do this. i will be absolutely grateful for any suggestions, agnieszka

A: 

Can you give a better idea of what you are trying to do?

XML > XSLT > produces aspx page

Sounds close to reinventing the windows presentation framework or XUL

Or is it ASPX reads xml > uses XSLT to add DOM elements to page... Sounds like AJAX

You want to write out a unique ID using the attribute transform http://www.w3schools.com/XSL/el_attribute.asp

david valentine
A: 

Could be tricky with a pure XSL solution.

You might be able to call a template which iterates the xml nodes you are using generate the controls, and writes out a c#/VB script block which adds them to a container of your choice.

Another option could be to add msxsl:script to your template, and use c# or another language to generate the output you want. This can sometimes be easier than a pure xsl solution, but does come with a performance cost.

It might be worth having a look at the source code of umbraco, which utilises xsl pretty heavily, and possibly already does what you are looking for.

seanb
+2  A: 

Once you have transformed your XML using XSLT, you could pass the output to the ASP.Net ParseControl method and it will return your controls ready to use. For example this code will place two buttons on the page:

protected void Page_Load(object sender, EventArgs e)
{
    // Fetch your XML here and transform it.  This string represents
    // the transformed output
    string content = @"
        <asp:Button runat=""server"" Text=""Hello"" />
        <asp:Button runat=""server"" Text=""World"" />";

    var controls = ParseControl(content);

    foreach (var control in controls)
    {
        // Wire up events, change settings etc here
    }

    // placeHolder is simply an ASP.Net PlaceHolder control on the page
    // where I would like the controls to end up
    placeHolder.Controls.Add(controls);
}
Generic Error
Ha! Then I learned something new and useful from SO today as well! :-)
Rune Grimstad
I tried this and get: foreach statement cannot operate variable of type System.Web.UI.Control because System.Web.UI.Control does not contain a public definition for 'GetEnumerator'
James Campbell
A: 

Thanks for all the answers.

This is what i do (it's not my code, but i'm doing it the same way):

private void CreateControls() { XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml"));

// Load the xslt to do the transformations
XslTransform transform = new XslTransform();
transform.Load(Server.MapPath("MakeControls.xslt"));

// Get the transformed result
StringWriter sw = new StringWriter();
transform.Transform(surveyDoc, null, sw);
string result = sw.ToString();

// parse the control(s) and add it to the page
Control ctrl = Page.ParseControl(result);
form1.Controls.Add(ctrl);

}

First solution (from Generic Error) is not good enough, because i need to identify the controls, for example during xslt transformation i will create 3 groups of controls, all having different ids. I would like to put references to each control from a group in different hashtable so that i would know later which controls are in each group.

The best solution would be to do it somehow when creating a control (so in xslt code..) but i don't know if it's possible.