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

Last Reply 3 months ago By pandeyism

Posted 3 months 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 3 months ago Modified on 3 months ago

Hey Bharath143,

Please refer below sample.

HTML

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

Namespaces

C#

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

VB.Net

Imports System.IO
Imports System.Web.Hosting

Code

C#

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

VB.Net

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 & """"))
    Next
    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))
    Next
    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

Screenshot