Monday, March 22, 2010

Load Html Page inside .aspx page

Well, there are couple of considerations
  1. Method 1: If you can thrust the html you can use a simple Web Control (I have called it as HTMLContent)
  2. Method 2: If you can't thrust the html you can use IFrame and use JQuery to append the IFrame content using your html conent
Implementation : Asp.Net 4.0 (C#)
Html Content Web Control

public class HtmlConent : WebControl
{
    public string Html { getset; }
    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.Write(string.Format("<div class=\"{0}\">"this.GetType().Name));
        base.RenderBeginTag(writer);
    }
    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.Write(Html);
    }
    public override void RenderEndTag(HtmlTextWriter writer)
    {
        base.RenderEndTag(writer);
        writer.Write("</div>");
    }
}

Test page : Both Method 1 and Method 2 included

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Register Namespace="Active.Web.UI.Controls" 
           Assembly="Active.Web.UI.Controls" TagPrefix="active" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
           Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Test Page</title>
    <script src="Scripts/jquery-1.4.1.min.js" 
           type="text/javascript" language="javascript"></script>
    <script runat="server">
        private string startupScript = @"
            $(document).ready(function () {{
                $(""body"", $(""iframe.ExternalPage"").contents()).html(""{0}"");
            }});
        ";       
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string page = @"
            <html>
                <head>
                    <title>External Html Page Title<title>
                </head>
                <body>
                    <div>
                        <h1>External Html Page Title</h2>
                        <p>External Html Page Content</p>
                    </div>
                </body>
            </html>
            ";
            int index = page.IndexOf("<body")+6;
            string content = page.Substring(index, page.IndexOf("</body") - index);
            ///
            /// Method 1
            ///
            hcContent.Html = content;
            ///
            /// Method 2
            ///
            this.Page.ClientScript.RegisterStartupScript(
                   this.GetType(), this.GetType().Name,
                   string.Format(this.startupScript, 
                   content.Replace("\"""'")
                          .Replace("\n",string.Empty)
                          .Replace("\r",string.Empty)), true);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <active:HtmlConent runat="server" ID="hcContent" />
        <iframe runat="server" id="iframeConent" class="ExternalPage" />
    </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...