ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Programmatically adding page on Button Click using C# and VB.Net in ASP.Net<p>Hey <a class="username" rel="Bharath143">Bharath143</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; FileName: &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtFileName&#34; /&gt; &lt;asp:Button Text=&#34;Create&#34; runat=&#34;server&#34; OnClick=&#34;Create&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Web.Hosting;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Web.Hosting </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Create(object sender, EventArgs e) { // aspx string aspxPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx"); string[] aspxContents = File.ReadAllLines(aspxPath); List&lt;string&gt; aspx = new List&lt;string&gt;(); for (int i = 0; i &lt; 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&lt;string&gt; cs = new List&lt;string&gt;(); for (int i = 0; i &lt; 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"); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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=""" &amp; txtFileName.Text &amp; ".aspx.cs""").Replace("Inherits=""CS""", "Inherits=""" &amp; txtFileName.Text &amp; """")) 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 " &amp; txtFileName.Text)) Next File.WriteAllLines(Server.MapPath("~/" &amp; txtFileName.Text &amp; ".aspx"), aspx.ToArray()) File.WriteAllLines(Server.MapPath("~/" &amp; txtFileName.Text &amp; ".aspx.cs"), cs.ToArray()) Response.Redirect(txtFileName.Text &amp; ".aspx") End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://imgur.com/f64lzpS.gif" alt="" width="472" height="65" /></strong></span></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/161003/Programmatically-adding-page-on-Button-Click-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/161003/Programmatically-adding-page-on-Button-Click-using-C-and-VBNet-in-ASPNet/Thu, 06 Dec 2018 03:58:21 GMT