Creating new webform in asp.net 4 on button click ?

I am stuck at a situation. How can I create new webform in Visual Web Developer 2010 each time a user clicks a button.I mean a new webform should be created each time a button is clicked. It should be saved forever like other webforms in the same website. The new  webform which is created should have the properties of an existing masterpage. Please help me with this.

paulcauchon4 years ago
If I'm understanding you correctly, your intention is to allow the dynamic creation of pages that would then be worked into your website?

Create a template web form and code behind file from which yours will be copied. Since ASP allows access to all of the .NET objects, you would need to write a function that would make a copy of that template form (and its code behind file) with a new name in the your site's directory or the directory to hold these pages.

ASPX Control:
<asp:Button id="NewPageButton" runat="server" OnClick="NewPageButton_Click" Text="Create a New Page" />

Thrown together, untested C# code:
using [all your libraries]
using System.IO

public partial class PageGenerator : [base page code file if you have one]
{
     protected void Page_Load(object sender, EventArgs e)
     {
          //page load code
     }

     protected void NewPageButton_Click(object sender, EventArgs e)
     {
          DateTime dtNow = DateTime.Now;
          StreamReader sr = new StreamReader("~/TemplateFile.aspx");
          StringBuilder sb = new StringBuilder();
          StreamWriter sw = new StreamWriter("~/" + dtNow.ToString(@"yyyymmddhhmmss") + ".aspx");
          String throwaway = sr.ReadLine();
          sw.WriteLine('<%@ Page Language="C#" AutoEventWireup="true" CodeFile="' + dtNow.ToString(@"yyyymmddhhmmss) + '.aspx.cs" Inherits="' + dtNow.ToString(@"yyyymmddhhmmss) + '" %>');
          sb.Append(sr.ReadToEnd());
          sw.Write(sb.ToString());
          sw.close(); sw.dispose(); sr.close(); sr.dispose();
          //now for the code behind file, same concept
          StreamReader sr2 = new StreamReader("~/TemplateFile.aspx.cs");
          StreamWriter sw2 = new StreamWriter("~/" + dtNow.ToString(@"yyyymmddhhmmss") + ".aspx.cs");
          StringBuilder sb2 = new StringBuilder();
          int numberOfUsings = 5; //replace this with the number of using statements in the code behind file
          for (int i = 0; i <= (numberOfUsings + 1); i++)
          {
               string throwAway2 = sr2.ReadLine();
          }
          sw2.WriteLine("using library1");
          sw2.WriteLine("using library2");
          sw2.WriteLine("using libraryN"); //add all of your using statements to the file
          sw2.WriteLine("public partial class " + dtNow.ToString(@"yyyymmddhhmmss") + " : [base page code file if you have one] "); //this is the code behind's class definition
          sb2.Append(sr2.ReadToEnd());
          sw2.Write(sb2.ToString());
          //send the user away to the new page
          Response.Redirect("~/" + dtNow.ToString(@"yyyymmddhhmmss" + ".aspx");
     }
}

I would be very cautious about putting a page like this on the internet because it's essentially an invitation for people to spam your server by creating page after page.

A Note:
A better method may be to connect your website to a database and then dynamically populate just one page with content from the database. That way you only have to worry about one physical page, and as much dynamic content as you would want (or could hold in a database) and can be served into a content control.
flyingdutchmen1659 (author) 4 years ago
i dont think its helpfull to me in any way thanks
Have you checked the tutorials offered on the ASP.net learning page?

http://www.asp.net/web-forms