Dynamically creating aspx page
Creating ASPX Page Dynamically in ASP.Net and C#:
Before some time I saw a logical question in a web site in
this a person give the problem as like this
“I have one page called First.aspx in my web application, in
this page; I have a simple asp.net button control. And I want is, on the click
event of that button control, create (render) a new dynamic "page"
that can be opened in a new windows or tab”
.
Here we give An Example here to creating a new .aspx page at
run time. We also give the option to give page name. The user can give as
he/she like. Like Google blogging we make new page at runtime.The new page is
not in the website, this page create needs to be created at runtime and needs
to be dynamic.
How to create a aspx page dynamically at runtime:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="CreateDynamicPage.aspx.cs"
Inherits="CreateDynamicPage"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Create Aspx Page Dynamically</title>
<style type="text/css">
.style1
{
width: 140px;
}
.style2
{
color: #990000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<h1 class="style2">Create Aspx Page Dynamically
by C#</h1>
<table style="width: 455px; height: 152px">
<tr>
<td align="right"><b>Enter Page Name:</b></td><td class="style1"><asp:TextBox ID="txtpagename"
runat="server"
/></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnCreate"
Text="Create ASPX
Page" runat="server"
onclick="btnCreate_Click" Width="141px" Height="35px"
style="color: #FFFFFF; font-weight: 700; background-color: #990000"
/>
<br />
<asp:Button ID="btnRedirect" Text="Redirect toPage" runat="server"
onclick="btnRedirect_Click" Width="141px" Height="38px"
style="color: #FFFFFF; font-weight: 700; background-color:
#990000" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblmsg" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Create a ASPX Page Dynamically in ASP.NET using C#:
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 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>"
" </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");
}
}
Create the .aspx page dynamically at runtime in ASP:
Download Full code ..
Jquery Related Other post:
- Example jQuery Validate on CheckBoxList using C#
- Limitation of Characters in Asp.net Textbox ,TextArea in asp.net
- How do you do html text encodes using JavaScript
- jQuery
- Example of jQuery Validate on Radiobuttonlist in Asp.Net using C#
- Validate ASP.Net RadioButtonList using JavaScript Example
- Example of jQuery Validate on Radiobuttonlist in Asp.Net using C#.
- Check Uncheck all asp.net CheckBox in asp.net using jQuery
- Ckeck box list example using javascript.
- Cropping image using jQuery in asp.net
- Displaying the textbox value in javascript Messagebox
- Get selected radio button values using JQuery
- fill data into Dropdown list by using Jquery
- jQuery Crop Image in Asp.net using Jcrop jQuery
- Example jQuery Validate on CheckBoxList using C#
- Asp.net CheckBoxList using jQuery.
- Get selected radio button values using JQuery.
Examples and other post related to Asp.net
- Select option to the user from many items.
- Display tooltip from database for dropdownlist items in asp.net c# example
- Show tooltip dropdownlist items by mouse over event using C# example
- Example of Adding ToolTip for each Dropdown List Item in C#.
- Checkboxlist in asp.net(control example)
- Example jQuery Validate on CheckBoxList using C#
- Check Uncheck all html CheckBoxlist controls using jQuery:
- Asp.net checkboxlist control example
- How to use CheckBox control in asp.net
- How to check If checkbox Is checked in asp.net
- Limitation of Characters in Asp.net Textbox ,TextArea in asp.net
- Validation checkbox control using JavaScript:
- Checkbox list Example using javascript
- Checkboxlist control in asp net.
- Asp.net Checkbox List.
- Asp.net CheckBoxList using jQuery
- Get Asp.net CheckBoxList control values using Jqury
It might help to others
ReplyDeletehttp://www.code-sample.com/