2

I have an XSLT transform to output a table of key/value pairs:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="keys">
    <table border="1">
      <xsl:apply-templates select="key" />
    </table>
  </xsl:template>
  <xsl:template match="key">
    <tr>
      <td>
        <xsl:value-of select="@name" />
      </td>
      <td>
        <pre>
          <xsl:value-of select="." />
        </pre>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

I am currently using it with an <asp:Xml /> server control.

The dynamic nature of the asp.net page cycle makes me think it might be possible to generate actual server side tags and get a sort of on-the-fly code generation functionality. Is this something that is possible/a good idea?

So the XSLT would contain:

  ...
        <asp:Label runat="server"><xsl:value-of select="@name" /></asp:Label>
  ...

Note: the motivation of my asking is more academic than anything else.

3 Answers 3

3

You can definitely do this.

You need to do this in the PreInit PreInit stage of your page lifecycle. I think if you aren't using viewstate you may be able to do it in PreLoad but I'm not 100% on that.
As far as it being a good idea it depends on why you want to do it.

If you are doing it just to do it then it is probably a bad idea :) You are then just making your application overly complex. If it meets some functional needs you require then it's a great idea and an elegant solution. :)

Addition: Here is a site that I had seen a while back with an example for you. XSLT To generate .NET tags

Sign up to request clarification or add additional context in comments.

3 Comments

are you saying that PreInit is before the ASPX markup is parsed, and I can change it there?
Yeah if you read the first link you can see controls and such aren't loaded till load. However I'd suggest you follow the 2nd link. It uses the load event to create the controls needed. I would recommend 1 change to that which i will put in a separate comment.
If you follow the 2nd link, it is a bit old, I would change XslTransform to XslCompiledTransform. Furthermore if you are using the same xsl sheets over and over i would suggest you cache the result of XslCompiledTransform it will save a lot of overhead in your application :)
2

Yes, you can define your XML in your own way and then translate those tags to server side controls using XSLT. Here is a basic example...

<section type="Panel" id="dyna_panel_Riskfactors" GroupingText="Identified Risk factors:" Width="200px">
   <control type="CheckBoxList" id="dyna_chkl_Riskfactors" CssClass="" ToolTip="Identified Risk factors">
     <subcontrol Type="ListItem" Text="Tattooing" Value="Tattooing" Selected="False" />
     <subcontrol Type="ListItem" Text="IV drug user" Value="IV drug user" Selected="False" />
     <subcontrol Type="ListItem" Text="Dont Know" Value="Dont Know" Selected="False" />
   </control>
</section>

and then transform the XML into a web page or a part of a web page using the following code...

  <!--For CheckBoxList-->
  <xsl:if test="@type='CheckBoxList'">
    <asp:CheckBoxList id="{@id}" CssClass="{@CssClass}" runat="server" ToolTip="{@ToolTip}" Width="{@Width}">
      <xsl:for-each select="subcontrol">
        <asp:ListItem Value="{@Value}" Selected="{@Selected}">
          <xsl:value-of select="@Text" />
        </asp:ListItem>
      </xsl:for-each>
    </asp:CheckBoxList>
  </xsl:if>

This way you can create a server control...

3 Comments

is that Razor syntax or a type of asp.net markup I'm unaware of?
@Gabriel that is the short-hand xslt syntax; you should be using that frequently in xslt (when mapping attributes) - it is a massive time-saver. It has nothing to do with asp.net or razor, and is solely xslt-related. So <foo id="{@id}"/> is similar to <foo><xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></foo>
Thank you, Marc =- that's awesome to know.
0

No; you are mixing two platforms there. If you did a lot of work with external methods (making your own .NET methods available in the xslt) you could probably make it work, but I think that would be extremely ugly. If you are using xslt, you should probably think of it more like MVC - you are generating the raw output (html), not server-side objects.

4 Comments

Not suggesting the OP do this per se but why can't he/she generate the controls in preinit?? He/she is just generating the "markup" to be processed in the pageload event.
@Jordan because xslt is not intended for creating asp.net controls; it is transform tool. Like I mentioned, you could provide an external object into the xslt processor for doing it, but: xslt is the key thing here.
xslt it intended for transforming XML into other forms.... asp.net controls fit nicely into that. As far as it's ability to do transformation it seems to fall within it's domain. I wouldn't say xslt is intended for any particular type of transformation.
Also please don't construe this as my saying the OP ought to do it. Just because a tool is there doesn't mean it is the right one. I'm just saying it is a tool in the toolbox that IMO fits some situations just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.