1

Whilst trying to capture the HTML from a Web.UI.Page at design-time (in a UITypeEditor) using the RenderControl method, I noticed that only server controls were being rendered.

So, given this aspx page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>WebForm1</title>
</head>
<body>
<form id="form1" runat="server">

    Some text

    <asp:Label runat="server" ID="label1">label1</asp:Label> 

    <div>
        <cc1:MyControl ID="MyControl1" runat="server" BorderStyle="Solid" Height="117px" Width="341px" />
    </div>    

    <div style="margin-top:30px;">
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>    

</form>
</body>
</html>

The RenderControl method would render 'label1', 'MyControl1' and 'Button1'. The head and all the divs in the form are all ignored.

I decided to look at the control tree for the page at design-time and this is what I got:

Page
 Label
 MyControl
 Button
 HtmlForm
  LiteralControl

This is what it looks like at run-time:

webform1_aspx
 LiteralControl
 HtmlHead
  HtmlTitle
 LiteralControl
 HtmlForm
  LiteralControl
  Label
  LiteralControl
  MyControl
  LiteralControl
  Button
  LiteralControl
 LiteralControl

Any ideas what is happening here?

Jules
  • 4,319
  • 3
  • 44
  • 72
  • http://www.west-wind.com/weblog/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages or `reader = new StreamReader(System.Net.WebRequest.Create(page.Path).GetResponse().GetResponseStream()); string html = reader.readToEnd(); ` – Tim Schmelter Jan 26 '12 at 11:25
  • Hi, I've tried the WebRequest method but I can't find a property called 'Path' for the Page class. I tried hardcoding the .aspx path in and this worked but the response is the orginal .aspx file contents. ie. the server controls have not been converted to html. – Jules Jan 26 '12 at 13:34
  • I can specify the development server address for the url and that works to a certain extent. eg. url='http://localhost:12345/WebForm1.aspx'. There are a few problems. Firstly the css classes are not picked up. Secondly I'll need to find a way to retrieve the port number (and the project must have been run at least once). Thirdly, I think some development environments use something called IIS?? I've no idea if that has a similar address to the development server. – Jules Jan 26 '12 at 13:46
  • I've got the server port now using HttpContext.Current.Request.ServerVariables("SERVER_PORT"). Is the development url the same even if using IIS? ie. localhost:/myform.aspx – Jules Jan 26 '12 at 13:55

0 Answers0