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.
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:
In
previous articles I explained Create table dynamically in asp.net and
bind to gridview, Get name of the day from date in c#, Prevent sql injection attacks in asp.net, Asp.net convert numbers to words in c#, Display list of files from folder in asp.net gridview and many articles
relating to asp.net, c#,
vb.net,
SQL server. Now I will explain how to create aspx page dynamically in asp.net
using c#, vb.net.
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
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. |
|||
|
|||
10 comments :
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 .
Nice one!!!
It s realy nice.How can be changed this in order to work this as a new page with some required styles?
how to add controls to dynamically created web page ?
Please write about mvc 3 tier ..
How to start...
Thanks ..
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...........
nice example sir but how can managed in mvc and also database
No sanjeev yadav not everyone uses MVC. Webforms will be around till Microsoft becomes bankrupt. I wonder if MVC is still supported?
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
It won't let me add a master page...
Note: Only a member of this blog may post a comment.