I added a page "First.aspx" in my website application. Inside First.aspx page i have a button named btnbutton. "onclick" Event of "btnbutton" a new dynamic page should open. how can i do this.? Please Remember the new dynamic page created, is not in existing in the application. This page should be created at runtime and dynamic also. please help me out!
Asked
Active
Viewed 1.5k times
4
-
As far as I know, there is nothing as the "Dynamicly created ASP.NET pages" you are asking about ... – Akram Shahda Jun 01 '11 at 08:46
-
Akram Shahda: i dont think so – AB Vyas Jun 01 '11 at 08:49
-
2It would be much better if you will explain in plain English ( without programming jargon ) what are you trying to achieve. – eugeneK Jun 01 '11 at 08:51
-
@eugeneK: I would like to open new page in runtime and that on that page i want one textbox and one button – AB Vyas Jun 01 '11 at 08:57
-
@amitvyas100688: To open an ASP.NET page, the code behind must be compiled. How would you compile the code of the generated page ?? – Akram Shahda Jun 01 '11 at 08:58
-
@Akram Shahda: That i dont knw so that i ask this question brother – AB Vyas Jun 01 '11 at 08:59
-
@amitvyas100688, you still using programming jargon because i'm 90% sure you not trying to do what you are saying you want. – eugeneK Jun 01 '11 at 08:59
-
@amitvyas100688: That's logicaly impossible. I dont know if there is any unlogical solution for it ... – Akram Shahda Jun 01 '11 at 09:00
-
@Akram Shahda: ok thnx brother i try to do that thing if i get solution i give u ok – AB Vyas Jun 01 '11 at 09:02
-
First.aspx is a dynamic page already, just have it reload showing your textbox and button instead when you click the link. – Oskar Duveborn Jun 01 '11 at 09:44
2 Answers
2
If you are asking about generating ASP.NET pages at runtime, that is impossible. The reason is the following:
You need to compile the code of the
ASP.NET
page before run it. And that is impossible after your web application has started.
However, if you are asking about navigation between pages, then, you could use Response.Redirect
:
Response.Redirect("http://www.stackoverflow.com/");

Akram Shahda
- 14,655
- 4
- 45
- 65
-
that i know about Navigation bro but i want to create a new page after my web application started. :( – AB Vyas Jun 01 '11 at 09:06
2
You can create new Dynamic File in ASP.net website (Not in a ASP.net Web Application). But there is a problem . Once you created a file the whole Website will be restarted for compiling the newly created file. So you Session data will be lost.
Here is the code to create new file.
string fielName = Server.MapPath("~/file.aspx");
//File.Create(fielName);
//File.AppendText(fielName);
// create a writer and open the file
TextWriter tw = new StreamWriter(fielName);
// write a line of text to the file
tw.WriteLine(@"<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeFile=""file.aspx.cs"" Inherits=""file"" %>
<!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 runat=""server"">
<title></title>
</head>
<body>
<form id=""form1"" runat=""server"">
<div>
</div>
</form>
</body>
</html>
");
// close the stream
tw.Close();
tw = new StreamWriter(fielName + ".cs");
// write a line of text to the file
tw.WriteLine(@"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 file : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(""new File "");
}
}
");
// close the stream
tw.Close();

Sreekumar P
- 5,900
- 11
- 57
- 82
-
suppose i have to give dynamic name of my page written in my text box and also change the name of CodeFile="" Inherits="" which is in html page what shall i do?? i have tried CodeFile="""+Textbox1.Text+""" Inherits="""+Textbox1.Text+""" but showing error – Mitesh Jain Jun 25 '13 at 11:24
-
1wht error you are getting..? you have to change the name here also .. `Server.MapPath("~/file.aspx")` – Sreekumar P Jun 25 '13 at 18:08
-
The error i am getting is:- Compiler Error Message: CS1010: Newline in constant // write a line of text to the file tw.WriteLine(@"<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeFile="""+TextBox1.Text+".cs"" Inherits="""+TextBox1.Text+""" %> – Mitesh Jain Jun 26 '13 at 12:26
-
this is just a problem with formatting the literal string. Try this code `tw.WriteLine(@"<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeFile=""" + TextBox1.Text + @".cs"" Inherits=""" + TextBox1.Text + @""" ` – Sreekumar P Jun 26 '13 at 23:12