Home > ASP.NET > How to use ASP.NET controls inside XSLTs

How to use ASP.NET controls inside XSLTs


Test.xslt:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:asp="remove"
    exclude-result-prefixes="msxsl vt">

    <xsl:output method="html" version="4.0"/>

    <xsl:template match="/">
        <div>
            <asp:TextBox runat="server"
              ID="txtname"></asp:TextBox>
        </div>
    </xsl:template>
</xsl:stylesheet>


Default.aspx.cs:

private void Page_Init(object sender, System.EventArgs e)
{
     XsltArgumentList xsltArgs = new XsltArgumentList();
     StringBuilder sb = new StringBuilder();
     StringWriter sw = new StringWriter(sb);
     XslCompiledTransform xml = new XslCompiledTransform();
     xml.Load(Server.MapPath("test.xslt"));
     xml.Transform(Server.MapPath("test.xml"), xsltArgs, sw);

    string html = sb.ToString();
    html = html.Replace("xmlns:asp=\"remove\"", String.Empty);//Retirar o namespace
    Control ctrl = ParseControl(html);//Get the control collection
    OutPutPlace.Controls.Add(ctrl);// OutPutPlace is a placeholder

    // Register some events
    TextBox txtname = OutPutPlace.FindControl("txtname") as TextBox;
    // Find a Textbox that was inside the XSLT
   }

Default.aspx:

<%@ Page Trace="true" language="c#"
    Codebehind="C.aspx.cs" AutoEventWireup="false" Inherits="default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>C</title>

    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <div>
              <asp:PlaceHolder ID="OutPutPlace" runat="server"></asp:PlaceHolder>

            </div>
        </form>
    </body>
</HTML>
Advertisements
Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: