Wednesday, March 21, 2012

XLST and control toolkit

I have been learning about using an XML file and an XLST file to create a user editable form however I have yet to find a successful working version of this including a toolkit procedure

Can it be done? If so has anyone actually done it and showed how? If not why not?

A not correctly formatted example of what I want to do:

<XML File>

<Input Name="NumbersOnly" FilteredTextBox="Yes" Type="NumbersWithNeg"/>

</XML>

gets me the output of this:

<

asp:TextBoxID="TextBox3"runat="server"></asp:TextBox>

<

ajaxToolkit:FilteredTextBoxExtenderID="ftbe"runat="server"TargetControlID="TextBox3"FilterType="Custom, Numbers"ValidChars="-"/>

It can be done. Heres how

<Code on main page>

private

void CreateSurvey() {// Load the data sourceXPathDocument surveyDoc =newXPathDocument(Server.MapPath("ExSurvey.xml"));// Load the xslt to do the transformationsXslTransform transform =newXslTransform();

transform.Load(Server.MapPath(

"MakeControls.xslt"));// Get the transformed resultStringWriter sw =newStringWriter();

transform.Transform(surveyDoc,

null, sw);string result = sw.ToString();// remove the namespace attribute

result = result.Replace(

"xmlns:asp=\"remove\"","");

result = result.Replace(

"xmlns:cc1=\"remove\"","");

result =

"<%@. Register Assembly=\"AjaxControlToolkit\" Namespace=\"AjaxControlToolkit\" TagPrefix=\"cc1\" %>" + result;int i;// parse the control(s) and add it to the pageControl ctrl = Page.ParseControl(result);

survey.Controls.Add(ctrl);

}

<XLST File>

<

xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:asp="remove"xmlns:cc1="remove">

<

xsl:outputmethod="xml"indent="yes"encoding="utf-8"omit-xml-declaration="yes"/>

<

xsl:templatematch="/">

<

table>

<

xsl:for-eachselect="//question">

<

tr>

<

tdvalign="top"><xsl:value-ofselect="@.name" />

<

xsl:iftest="@.required = 'yes'">

<

asp:RequiredFieldValidatorErrorMessage=" Required Field"runat="server"ControlToValidate="{@.name}" />

</

xsl:if>

</

td>

<

td>

<

xsl:iftest="@.type='text'">

<

asp:TextBoxid="{@.name}"runat="server" />

<

cc1:FilteredTextBoxExtenderID="FilteredTextBoxExtender{@.name}"runat="server"TargetControlID="{@.name}"FilterType="Numbers">

</

cc1:FilteredTextBoxExtender>

</

xsl:if>

<

xsl:iftest="@.type='radio'">

<

asp:RadioButtonListid="{@.name}"runat="server">

<

xsl:for-eachselect="choice">

<

asp:ListItemValue="{@.value}"><xsl:value-ofselect="@.value" /></asp:ListItem>

</

xsl:for-each>

</

asp:RadioButtonList>

</

xsl:if>

</

td></tr>

</

xsl:for-each>

</

table>

<

asp:buttonid="submit"runat="server"Text="Submit" />

</

xsl:template>

</

xsl:stylesheet>

No comments:

Post a Comment