aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Create a ASPX Page Dynamically in ASP.NET using C#.NET, VB.NET

Aug 22, 2014

Here I will explain
how to create aspx page dynamically in using c#, or Dynamically create aspx page in your project at runtime in using c#,

Suppose you are doing a project and one of your page name is page.aspx. To show your data into that page you are using query string like page.aspx?id=9 or . Here in this your SEO is getting weak due to the URL of your page. To prevent this kind of problem you can create a new virtual page with your own URL and show the content as per your database.

So how to do this...Don't worry very simple few steps you have to follow.

First create one new web application and open your Default.aspx and write the following code

<html xmlns="">
<head runat="server">
<title>Create Aspx Page Dynamically in</title>
<form id="form1" runat="server">
<td><b>Enter Page Name:</b></td><td><asp:TextBox ID="txtpagename" runat="server" /></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>
<asp:Label ID="lblmsg" runat="server" />

Now in code behind file write the code like as shown below

C# Code

using System;
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>",
"<title>The New Page</title>",
"   <form id=\"form1\" runat=\"server\">",
"       <div>",
"           <asp:literal id=\"output\" runat=\"server\"/>",
"       </div>",
"   </form>",
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");


Imports System.IO
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim aspxLines As String() = {"<%@ 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>", _
Dim csLines As String() = {"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"
End Sub
Protected Sub btnRedirect_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("" + txtpagename.Text.Trim() + ".aspx")
End Sub
End Class

Now run above code and check the output that will be like as shown below same way if you want to insert or fetch data from database or anything you can add that code in code behind during page creation.


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

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...


I am sanjeev yadav,I am Software Engineer . I wolud to say why don''nt u use MVC.

Now-a-days Every website is made in MVC pattern. So Please Give Example in MVC so that we can understand MVC Project easily .

MVC and Entity Framework and also pls tell how to sue Web api in MVC .

Eeram Hareesh said...

Nice one!!!

Anonymous said...

It s realy nice.How can be changed this in order to work this as a new page with some required styles?

Anonymous said...

how to add controls to dynamically created web page ?

pulkit said...

Please write about mvc 3 tier ..
How to start...
Thanks ..

rakesh ranjan said...

Thanks a lot for this post but i need to create dynamic pages in html format. please help me in this way with all fields like meta tag, header, body and so on...........

jishan siddique said...

nice example sir but how can managed in mvc and also database

Billy Wagner said...

No sanjeev yadav not everyone uses MVC. Webforms will be around till Microsoft becomes bankrupt. I wonder if MVC is still supported?

Soheb Baig said...

Thank you sir , this blog has helped me alot in creating my project for final year (TYBscIT) ,I have altered the code as per my requirements : now what i can do is as follows
Create a web page
Add n number of text boxes
And labels with them
Create a table
Add values to the table ( from the new page)
Get the values in an excel file (values which entered in new page created )
Delete the page and all the data associated with it.

If any one wants the code i will upload it on GITHUB later (in a month ~2nd week of may and post the link here )
Thank you sir again

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.