ASP.NET - controls generated by xslt transformation

I am dynamically generating controls in my asp.net page by converting xslt from xml file. I will need to reference these controls from the code behind it later. I would like to add these links to the / hashtable / what list during creation (in an xslt file, I suppose) so that I can contact them later and I have no idea how. I will be absolutely grateful for any suggestions, Agnieszka

+1


source to share


4 answers


Once you transform your XML using XSLT, you can pass it 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);
}

      

+2


source


Can you get a better understanding of what you are trying to do?

XML> XSLT> creates aspx page

Sounds close to rethinking the Windows or XUL presentation framework



Or is this ASPX reads xml> uses XSLT to add DOM elements to the page ... Looks like AJAX

You want to write the unique id using attribute transformation http://www.w3schools.com/XSL/el_attribute.asp

0


source


Can be tricky with a pure XSL solution.

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

Another option might be to add msxsl: script to your template and use C # or another language to generate the output you want. This can sometimes be simpler than a pure xsl solution, but it is performance related.

It might be worth taking a look at the source code for umbraco, which uses xsl quite heavily and may already do what you are looking for.

0


source


Thanks for all the answers.

This is what I am doing (this is not my code, but I am doing the same):

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);

      

}

The first solution (from Generic Error) is not good enough because I need to identify the controls, for example, during the xslt conversion, I will create 3 groups of controls, all of which have different IDs. I would like to put references to each control in a group into a different hash table so that I can later find out which controls are in each group.

The best solution would be to do it somehow when creating the control (so in the xslt .. code), but I don't know if this is possible.

0


source







All Articles