AB Vyas
AB Vyas

Reputation: 2389

how can i create a dynamic page in asp.net (C#)?

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!

Upvotes: 4

Views: 15958

Answers (2)

Sreekumar P
Sreekumar P

Reputation: 6050

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();

Upvotes: 2

Akram Shahda
Akram Shahda

Reputation: 14781

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/");

Upvotes: 2

Related Questions