Programmatically adding page on Button Click using C# and VB.Net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Hi All,

I wanted to generate multiple pages along with data using bootstrap.

i have a create form, along with copy, delete buttons. user click on the copy button entire page will be create as another page, on every copy click generate multiple pages.

I am looking for generate multiple pages along with data on button click.

i want to copy the entire page on button click  and dispaly the same page 

please help me out on this.

Posted one month ago Modified on one month ago

Hey Bharath143,

Please refer below sample.


    <asp:TextBox runat="server" ID="txtFileName" />
    <asp:Button Text="Create" runat="server" OnClick="Create" />



using System.IO;
using System.Web.Hosting;


Imports System.IO
Imports System.Web.Hosting



protected void Create(object sender, EventArgs e)
    // aspx
    string aspxPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx");
    string[] aspxContents = File.ReadAllLines(aspxPath);
    List<string> aspx = new List<string>();
    for (int i = 0; i < aspxContents.Length; i++)
        aspx.Add(aspxContents[i].Replace("CodeFile=\"CS.aspx.cs\"", "CodeFile=\"" + txtFileName.Text + ".aspx.cs\"").Replace("Inherits=\"CS\"", "Inherits=\"" + txtFileName.Text + "\""));

    // cs
    string csPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx.cs");
    string[] csContents = File.ReadAllLines(csPath);
    List<string> cs = new List<string>();
    for (int i = 0; i < csContents.Length; i++)
        cs.Add(csContents[i].Replace("class CS", "class " + txtFileName.Text));
    File.WriteAllLines(Server.MapPath("~/" + txtFileName.Text + ".aspx"), aspx.ToArray());
    File.WriteAllLines(Server.MapPath("~/" + txtFileName.Text + ".aspx.cs"), cs.ToArray());
    Response.Redirect(txtFileName.Text + ".aspx");


Protected Sub Create(ByVal sender As Object, ByVal e As EventArgs)
    Dim aspxPath As String = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx")
    Dim aspxContents As String() = File.ReadAllLines(aspxPath)
    Dim aspx As List(Of String) = New List(Of String)()
    For i As Integer = 0 To aspxContents.Length - 1
        aspx.Add(aspxContents(i).Replace("CodeFile=""CS.aspx.cs""", "CodeFile=""" & txtFileName.Text & ".aspx.cs""").Replace("Inherits=""CS""", "Inherits=""" & txtFileName.Text & """"))
    Dim csPath As String = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx.cs")
    Dim csContents As String() = File.ReadAllLines(csPath)
    Dim cs As List(Of String) = New List(Of String)()
    For i As Integer = 0 To csContents.Length - 1
        cs.Add(csContents(i).Replace("class CS", "class " & txtFileName.Text))
    File.WriteAllLines(Server.MapPath("~/" & txtFileName.Text & ".aspx"), aspx.ToArray())
    File.WriteAllLines(Server.MapPath("~/" & txtFileName.Text & ".aspx.cs"), cs.ToArray())
    Response.Redirect(txtFileName.Text & ".aspx")
End Sub