Click here to Skip to main content
15,791,963 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear All,

I encounter a puzzle, In my original logic the page rendered by Response.Output and via XML&XSLT, and in XSLT file where only have html controls tags.
Now, I need to modify the logic, make page display server control too, so use Response.Output I can't display the server control, and the server control tags in XSLT file can't parse by ASP.NET. How can I do to implement this case?

BTW, must use Response.Output to render html. as my code, the server control txt1 can't display.


--------------------the refer code and xml, xslt file as below --------------------
test.XML
---------
XML
<company>
    <name>XYZ Inc.</name>
    <address1>One Abc Way</address1>
    <address2>Some avenue</address2>
    <city>Tech city</city>
    <country>Neverland</country>
</company>


--------test.XSLT as below--------
XML
<xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="http://schemas.microsoft.com/Visual-Studio-Intellisense"
      version="1.0">

<!-- indicates what our output type is going to be -->
<xsl:output method="html" />

    <!--
        Main template to kick off processing our Sample.xml
        From here on we use a simple XPath selection query to
        get to our data.
    -->
    <xsl:template match="/">

        <style>
          body,td {font-family:Tahoma,Arial; font-size:9pt;}
        </style>
                <h2>Welcome to <xsl:value-of select="/company/name"/></h2>
                <p/>
                <b>Our contact details:</b>
                <br/>
                <br/>
                <xsl:value-of select="/company/name"/>
                <br/>
                <xsl:value-of select="/company/address1"/>
                <br/>
                <xsl:value-of select="/company/address2"/>
                <br/>
                <xsl:value-of select="/company/city"/>
                <br/>
                <xsl:value-of select="/company/country"/>
        <!--<input id="text1" type="text" value="A B C D E F G"/>-->
        <asp:textbox id="txt1" visible="false" runat="server" text="A B C D E F G H I J K L M N O P Q R S T"></asp:textbox>
  </xsl:template>

</xsl:stylesheet>


-------------ASPX.CS code as below-----------
<pre lang="C#">protected void Page_Load(object sender, EventArgs e)
   {
       this.TransForm();

   }
   protected override void Render(HtmlTextWriter writer)
   {
       base.Render(writer);
       BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField;
       ArrayList arr = (ArrayList)Response.Output.GetType().GetField("_buffers", bind).GetValue(Response.Output);
       Assembly systemWeb = Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
       Type type = systemWeb.GetType("System.Web.IHttpResponseElement");
       MethodInfo method = type.GetMethod("GetBytes");
       StringBuilder sb = new StringBuilder(5000);
       for (int i = 0; i &lt; arr.Count; i++)
       {
           byte[] buffer = (byte[])method.Invoke(arr[i], null);
           sb.Append(Response.ContentEncoding.GetString(buffer));
       }
       byte[] fromXSLT = (byte[])method.Invoke(arr[0], null);
       string fromXSLTStr = Response.ContentEncoding.GetString(fromXSLT);
       char[] charBuffer = (char[])Response.Output.GetType().GetField("_charBuffer", bind).GetValue(Response.Output);
       int charBufferLength = (int)Response.Output.GetType().GetField("_charBufferLength", bind).GetValue(Response.Output);
       int charBufferFree = (int)Response.Output.GetType().GetField("_charBufferFree", bind).GetValue(Response.Output);
       for (int i = 0; i &lt; charBufferLength - charBufferFree; i++)
       {
           sb.Append(charBuffer[i]);
       }
       this.Response.Clear();
       //Page.Controls.Add((Control)this.Page.ParseControl(sb.ToString()));
       string htmlStr = sb.ToString().Replace("xmlns:asp=\"http://schemas.microsoft.com/Visual-Studio-Intellisense\"&gt;", "&gt;");
       writer.Write(htmlStr);
   }
   public void TransForm()
   {
       string sourceDoc = @"D:/test.xml";
       string xsltDoc = @"D:/test.xslt";
       xmldom oXML = new xmldom();
       oXML.loadFile(sourceDoc);
       try
       {
           oXML.transformNode(xsltDoc, Response.Output);
       }
       catch (FileNotFoundException filexc)
       {
           throw new Exception("Can't find the special files.");
       }
   }

Posted
Updated 8-Mar-11 17:59pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900