Skip to main content

How To Include Content of One Page To another Page

In this post i will show how to include the content of one page inside another using Regular expression .
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblHtml" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      lblHtml.Text= ParseHtml();

    }
    private static string ParseHtml()
    {
        StreamReader sr;
        string html;
        sr = File.OpenText(HttpContext.Current.Server.MapPath("Default.aspx"));
        html = sr.ReadToEnd();
        sr.Close();

        Regex
            start = new Regex(@"[\s\S]*<body[^<]*>", RegexOptions.IgnoreCase);
        html = start.Replace(html, "");
        Regex end = new Regex(@"</body[\s\S]*", RegexOptions.IgnoreCase);
        html = end.Replace(html, "");
        return html;
    }
}

Comments

Popular posts from this blog