Friday, August 1, 2014

Create an ASPX page dynamically in ASP.NET C#

Hi, here in this example I will show you how to create a new ASPX page dynamically or virtually in ASP.NET using C#.

First create a new website and add a new page. In this page we will create a new page. Then take one TextBox and two Buttons, one for generating  and another one for redirecting to out new virtual page.

Now on the button click event write down the following code.


/// <summary>
/// Generate new page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
     string[] aspxLines = {"<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\""+TextBox1.Text.Trim()+".aspx.cs\" Inherits=\"generate_page_runtime."+TextBox1.Text.Trim()+"\" %>",
                        "<!DOCTYPE html>",
                        "<head>",
                        "<title>The New Page</title>",
                        "</head>",
                        "<body>",
                        "   <form id=\"form1\" runat=\"server\">",
                        "       <div>",
                        "           <asp:literal id=\"output\" runat=\"server\"/>",
                        "       </div>",
                        "   </form>",
                        "</body>",
                        "</html>"};
       string[] csLines = {"using System;",
                         "using System.Web.UI.WebControls;",
                         "namespace generate_page_runtime {",
                         "    public partial class "+TextBox1.Text.Trim()+" : System.Web.UI.Page {",
                         "        protected void Page_Load(object sender, EventArgs e) {",
                         "            output.Text = \"Our new page\";",
                         "        }",
                         "    }",
                         "}"};
       File.WriteAllLines(Server.MapPath("" + TextBox1.Text.Trim() + ".aspx"), aspxLines);
       File.WriteAllLines(Server.MapPath("" + TextBox1.Text.Trim() + ".aspx.cs"), csLines);
}


/// <summary>
/// Redirect to new virtual page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
     Response.Redirect("" + TextBox1.Text.Trim() + ".aspx");
}

And on the ASPX page write down the code.

<head runat="server">
    <title>Generate New Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>Geberate New Virtual Page</h1>
            Old page
        <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>

Run your project and generate your virtual pages. You can download the full source code here.

0 comments:

Post a Comment

Popular Posts

Pageviews