Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
Introduction:

Here I will explain
how to create aspx page dynamically in asp.net using c#, vb.net or Dynamically create aspx page in your project at runtime in asp.net using c#, vb.net.
Description:

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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create Aspx Page Dynamically in Asp.net</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>

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>",
"<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");
}
}

VB.NET Code


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>", _
"</html>"}
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.

Output



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 Asp.net, C#.net, 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

10 comments :

Anonymous said...

hi

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 .

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 ..

Test 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

Unknown said...

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

Unknown 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

P said...

It won't let me add a master page...

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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