Create a ASPX Page Dynamically in ASP.NET
This article will describe how to create aspx page dynamically in asp.net using c#.
First create one new web application and create a new page name "DynamicPage.aspx". open it and write the following code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>Enter Page Name:</b></td>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>Enter Page Name:</b></td>
<td><asp:TextBox ID="txtPageName" runat="server" /></td>
</tr>
<tr>
<td> </td>
<td> <asp:Button ID="btnCreate" Text="Create ASPX Page" runat="server"
onclick="btnCreate_Click" />
<asp:Button ID="btnRedirect" Text="Redirect to Dynamic Page" runat="server"
onclick="btnRedirect_Click" /></td>
</tr>
</table>
<asp:Label ID="lblmsg" runat="server" />
</form>
</body>
</html>
</tr>
<tr>
<td> </td>
<td> <asp:Button ID="btnCreate" Text="Create ASPX Page" runat="server"
onclick="btnCreate_Click" />
<asp:Button ID="btnRedirect" Text="Redirect to Dynamic Page" runat="server"
onclick="btnRedirect_Click" /></td>
</tr>
</table>
<asp:Label ID="lblmsg" runat="server" />
</form>
</body>
</html>
Write the following code in .CS Page
using System;
using System.IO;
public partial class DynamicPage: 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");
}
}
using System.IO;
public partial class DynamicPage: 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");
}
}
Now run the code.
No comments:
Post a Comment