Dynamically creating aspx page

Creating ASPX Page Dynamically in ASP.Net and C#:



Before some time I saw a logical question in a web site in this a person give the problem as like this
“I have one page called First.aspx in my web application, in this page; I have a simple asp.net button control. And I want is, on the click event of that button control, create (render) a new dynamic "page" that can be opened in a new windows or tab”
.
Here we give An Example here to creating a new .aspx page at run time. We also give the option to give page name. The user can give as he/she like. Like Google blogging we make new page at runtime.The new page is not in the website, this page create needs to be created at runtime and needs to be dynamic.

How to create a aspx page dynamically at runtime:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateDynamicPage.aspx.cs" Inherits="CreateDynamicPage" %>

<!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>Create Aspx Page Dynamically</title>
    <style type="text/css">
        .style1
        {
            width: 140px;
        }
        .style2
        {
            color: #990000;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<h1 class="style2">Create Aspx Page Dynamically by C#</h1>
<table style="width: 455px; height: 152px">
<tr>
<td align="right"><b>Enter Page Name:</b></td><td class="style1"><asp:TextBox ID="txtpagename" runat="server" /></td>
</tr>
<tr>

<td colspan="2" align="center">
    <asp:Button ID="btnCreate" Text="Create ASPX Page" runat="server"
onclick="btnCreate_Click" Width="141px" Height="35px"
        style="color: #FFFFFF; font-weight: 700; background-color: #990000" />
    <br />
<asp:Button ID="btnRedirect" Text="Redirect toPage" runat="server"
onclick="btnRedirect_Click" Width="141px" Height="38px"
        style="color: #FFFFFF; font-weight: 700; background-color: #990000" /></td>
</tr>
<tr>

<td colspan="2" align="center">
<asp:Label ID="lblmsg" runat="server" />
    </td>
</tr>
</table>
</div>
</form>
</body>
</html> 


Create a ASPX Page Dynamically in ASP.NET using C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class CreateDynamicPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string[] aspxLines = {"<%@ Page Language=\"C#\" AutoEventWireup=\"true\"CodeFile=\""+txtpagename.Text.Trim()+".aspx.cs\" Inherits=\"generate_page_runtime."+txtpagename.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 "+txtpagename.Text.Trim()+" : System.Web.UI.Page {",
"        protected void Page_Load(object sender, EventArgs e) {",
"            output.Text = \"Our new page\";",
"        }",
"    }",
"}"};
        File.WriteAllLines(Server.MapPath("" + txtpagename.Text.Trim() + ".aspx"), aspxLines);
        File.WriteAllLines(Server.MapPath("" + txtpagename.Text.Trim() + ".aspx.cs"), csLines);
        lblmsg.Text = "Aspx Page Created Successfully";
    }
    protected void btnRedirect_Click(object sender, EventArgs e)
    {
        Response.Redirect("" + txtpagename.Text.Trim() + ".aspx");
    }
}

Create the .aspx page dynamically at runtime in ASP:


 
Dynamically creating aspx page  in asp.net
Dynamically creating aspx page in asp.net

Download Full code ..

Jquery Related Other post:

Examples and other post related to Asp.net


Comments

Post a Comment