UMAIR ALI
UMAIR ALI

Reputation: 1115

how to create a .aspx page dynamically in asp.net

I have one page called First.aspx in website, int his page, I have a simple asp.net button.

What I want is, on the click event of that button, render a new dynamic "page" that can be opened in a new windows or tab.

How can I achieve this?

The new page is not in the website, this page create needs to be created at runtime and needs to be dynamic.

Upvotes: 1

Views: 13174

Answers (3)

UMAIR ALI
UMAIR ALI

Reputation: 1115

protected void Button1_Click(object sender, EventArgs e)
    {

        File.Copy(Server.MapPath("") + "\\abc.aspx", (Server.MapPath("") + "\\" + TextBox1.Text + ".aspx"));
        File.Copy(Server.MapPath("") + "\\abc.aspx.cs", (Server.MapPath("") + "\\" + TextBox1.Text + ".aspx.cs"));

        //.aspx Page

        StreamReader sr = new StreamReader(Server.MapPath("") + "\\" + TextBox1.Text + ".aspx");
        string fileContent = sr.ReadToEnd();
        sr.Close();
        using (StreamWriter Sw = new StreamWriter(Server.MapPath("") + "\\" + TextBox1.Text + ".aspx"))
        {
            fileContent = fileContent.Replace("abc",  TextBox1.Text);
            Sw.WriteLine(fileContent);
            Sw.Flush();
            Sw.Close();
        }

        //.aspx.cs Page

        StreamReader sr1 = new StreamReader(Server.MapPath("") + "\\" + TextBox1.Text + ".aspx.cs");
        string fileContent1 = sr1.ReadToEnd();
        sr1.Close();
        using (StreamWriter Sw1 = new StreamWriter(Server.MapPath("") + "\\" + TextBox1.Text + ".aspx.cs"))
        {
            fileContent1 = fileContent1.Replace("abc", TextBox1.Text);
            Sw1.WriteLine(fileContent1);
            Sw1.Flush();
            Sw1.Close();
        }

    }

Upvotes: 1

Rab
Rab

Reputation: 35572

I Would suggest NOT to. As You are using server side language. you don't need to physically create a page. you can dynamically display page contents based on the URL Type in by Implementing Routing on your website.

Upvotes: 1

BLoB
BLoB

Reputation: 9725

Not sure why you would want a physical page created when you could achieve similar results by storing information in a DB and have a generic page 'template' that reads from the db and populates the page depending on some other info such as the URL...

Anyway, here is a solution to your question, albeit somewhat old... creating aspx page dynamically in aspnet

Upvotes: 0

Related Questions