In this article i will explain how to create aspx page dynamically in asp.net using c#, vb.net. and also explain Dynamically create aspx page in your project at runtime in asp.net using c#, vb.net.
Assume that your are working on asp.net project and your page name is DemoPage.aspx. Now, To show your records/data/information into that page you are using query string like DemoPage.aspx?id=7. Here in this way your SEO (Search Engine Optimisation) is getting weak due to the URL of your webpage. To prevent this kind of problem you can create a new virtual dynamic page with your own URL and show the content as per your database.
But Question is how to do this ? Don't worry in this article i will explain how to do with simple example it is very simple you have to follow just only few steps.
So, Let's Create one Web page for Demonstration Purpose.
First you have to Create one web page with name Default .aspx and write following aspx code.
aspx 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 align="center"> <tr> <td><b style="font-size:large">Enter WebPage Name:</b></td> <td> <asp:TextBox ID="txt_Pagename" runat="server" Height="30px" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btn_CreatePage" runat="server" Text="Create ASPX Page" OnClick="btn_CreatePage_Click" Height="40px" BorderStyle="None" BackColor="Green" ForeColor="White" /> <asp:Button ID="btn_RedirectPage" runat="server" Text="Redirect to Created Dynamic Page" OnClick="btn_RedirectPage_Click" Height="40px" BorderStyle="None" BackColor="Blue" ForeColor="White" /></td> </tr> </table> <br /> <center><asp:Label ID="lbl_msg" Font-Size="x-Large" runat="server" /></center> </form> </body> </html>
Now, in the code behind file write the code like as shown below.
C# Code For Create aspx Page Dynamically
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 _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_CreatePage_Click(object sender, EventArgs e) { string[] aspxLines = {"<%@ Page Language=\"C#\" AutoEventWireup=\"true\"CodeFile=\""+txt_Pagename.Text.Trim()+".aspx.cs\" Inherits=\"generate_page_runtime."+txt_Pagename.Text.Trim()+"\" %>", "<!DOCTYPE html>", "<head>", "<title>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 "+txt_Pagename.Text.Trim()+" : System.Web.UI.Page {", " protected void Page_Load(object sender, EventArgs e) {", " output.Text = \"Hello, Welcome to ' <b style='color:blue'>" + txt_Pagename.Text.Trim() +".aspx </b>' This is Our new page\";", " }", " }", "}"}; File.WriteAllLines(Server.MapPath("" + txt_Pagename.Text.Trim() + ".aspx"), aspxLines); File.WriteAllLines(Server.MapPath("" + txt_Pagename.Text.Trim() + ".aspx.cs"), csLines); lbl_msg.Text = "Your Aspx WebPage '<b style='color:red'>" + txt_Pagename.Text.Trim() + ".aspx </b>' Created Successfully...!"; } protected void btn_RedirectPage_Click(object sender, EventArgs e) { Response.Redirect("" + txt_Pagename.Text.Trim() + ".aspx"); } }
Vb.Net Code For Create aspx Page Dynamically
Imports System.Web Imports System.IO Partial Class Default2 Inherits System.Web.UI.Page Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs) Dim aspxLines As String() = {"<%@ Page Language=""VB"" AutoEventWireup=""true""CodeFile=""" + txt_Pagename.Text.Trim() + ".aspx.vb"" Inherits=""generate_page_runtime." + txt_Pagename.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 " + txt_Pagename.Text.Trim() + " : System.Web.UI.Page {", " protected void Page_Load(object sender, EventArgs e) {", "output.Text = ""Hello, Welcome to ' <b style='color:blue'>" + txt_Pagename.Text.Trim() + ".aspx </b>' This is Our new page"";", _ " }", " }", "}"} File.WriteAllLines(Server.MapPath("" + txt_Pagename.Text.Trim() + ".aspx"), aspxLines) File.WriteAllLines(Server.MapPath("" + txt_Pagename.Text.Trim() + ".aspx.cs"), csLines) lbl_msg.Text = "Your Aspx WebPage '<b style='color:red'>" + txt_Pagename.Text.Trim() + ".aspx </b>' Created Successfully...!" End Sub Protected Sub btnRedirect_Click(ByVal sender As Object, ByVal e As EventArgs) Response.Redirect("" + txt_Pagename.Text.Trim() + ".aspx") End Sub End Class
Demo:
If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon