Creating Asp.net Page Dynamically using Dotnet Programe

HI friends

today iam going to discuss ” the Concept of creating asp.net page at Runtime with simple Trick ” 

First you have to create one empty page say “demopage.aspx” in your project along with your programe page

the Trick here is i will copy the existing page called demopage.aspx and rename it as what ever the page name i want to create

for example i have a textbox which takes name of page to create

and on clicking the button one should be able to create a page with the name given in textbox as shown in image :

write the following code in button_click()

Vb.net Code
File.Copy(Server.MapPath("") & "\\DemoPage.aspx", (Server.MapPath("") & "\\" + TextBox1.Text & ".aspx"))
C# code
File.Copy(Server.MapPath("") + "\\DemoPage.aspx", (Server.MapPath("") + "\\" + TextBox1.Text + ".aspx"))

the above StateMent means copy the file named DemoPage.aspx as the file name given in your Textbox

now do the same for code behind also

Vb.net code
File.Copy(Server.MapPath("") & "\\DemoPage.aspx.vb", (Server.MapPath("") & "\\" + TextBox1.Text & ".aspx.vb"))
C# code
File.Copy(Server.MapPath("") + "\\DemoPage.aspx", (Server.MapPath("") + "\\" + TextBox1.Text + ".aspx.vb"))

now your Physical file is ready but when you run this you will get an error 

saying that the newly created file is not having Code File “DempPage.aspx.vb” in newly created

for this you have to use the below method which will take the total content of the newly created file and searches for CodeFile=”DemoPage.aspx.vb”  replaces it with

your file name code behind CodeFile=”TextBoxName.aspx.vb etc

the Method should be written  like this

VB.net Code
Private Sub FindReplaceFileString(fileName As String, oldValue As String, newValue As String)

	Dim sr As New StreamReader(fileName)
	Dim fileContent As String = sr.ReadToEnd()
	sr.Close()
	Using Sw As New StreamWriter(fileName)
		fileContent = fileContent.Replace(oldValue, newValue)
		Sw.WriteLine(fileContent)
		Sw.Flush()
		Sw.Close()
	End Using
End Sub
C# code
private void FindReplaceFileString(string fileName, string oldValue, string newValue)
{
	StreamReader sr = new StreamReader(fileName);
	string fileContent = sr.ReadToEnd();
	sr.Close();
	using (StreamWriter Sw = new StreamWriter(fileName)) {
		fileContent = fileContent.Replace(oldValue, newValue);
		Sw.WriteLine(fileContent);
		Sw.Flush();
		Sw.Close();
	}
}

use this like this

FindReplaceFileString(TextBox1.Text+”.aspx”,”DemoPage”,TextBox1.Text)

when you write this it will work like this

it takes your file name which is given in textbox1 and goes to it performs method of replacing the content “DemoPage” to TextBox1.Text

and once you are done with till this step your programe will be working fine

FOr Example

if iam creating a file named Raju.aspx i will enter like this

on click event of the button write like this

File.Copy(Server.MapPath("") & "\\DemoPage.aspx", (Server.MapPath("") & "\\" + TextBox1.Text & ".aspx"))
FindReplaceFileString(TextBox1.Text+".aspx","DemoPage",TextBox1.Text)

so the above first line will create file for you and the next line will find DemoPage inside the Page and will Rewrite it with The TextBox ile, Raju

so this will create file and you can create it using either C# or Vb.net

thanks and Regards

kakani santosh kumar

July 02 2011

Note : One Should not Delete Original “DemoPage.aspx” from the Solution Explorer 

Advertisements

About kakani santosh kumar

Working as Software Engineer in Prithvi Information Solutions having 3 years of Experience in Dotnet Framework ranging from 1.1 to 3.5
This entry was posted in Uncategorized. Bookmark the permalink.

20 Responses to Creating Asp.net Page Dynamically using Dotnet Programe

  1. Do u have this code in C#

    • You can use
      this Site to change code from vb to C# just by copying and pasting code
      http://www.developerfusion.com/tools/convert/vb-to-csharp/
      any ways iam posting c # code for the same by modifying my article please have a look 🙂

      Gud luck

      • @santoshtherock : thx alot….
        I have one question regarding above code:
        What I understood is: add
        “File.Copy(Server.MapPath(“”) & “\\DemoPage.aspx”, (Server.MapPath(“”) & “\\” + TextBox1.Text & “.aspx”))” on click event of button..
        In second step u have written that “now do the same for code behind also”
        “File.Copy(Server.MapPath(“”) & “\\DemoPage.aspx.vb”, (Server.MapPath(“”) & “\\” + TextBox1.Text & “.aspx.vb”))”
        what do u mean by this?means which behind code??

  2. Yes
    the first code is to create Aspx file
    next
    same code is to create vb file which is code behind file for the above aspx file

    Example
    first one is for Default.aspx
    second one is for Default.aspx.vb which is code behind file

  3. What about compile and build?

    • .. Will Post it soon Mr Thomas ..
      the current article is all about creating a Page
      but once if you are using this asp.net Page which we created Dynamically that will automatically gets Compiled and displays Blank page output
      so i think one need not worry about compile and debug

      i will check it out once and will let you know

      all the best for you Mr. Thomas. Derenthal

  4. Udupi says:

    good blog

  5. ritu says:

    ‘C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\vbc.aspx’. whn im trying this …me geeting this error again and again…plz help to hw to overcome this

  6. arvind says:

    I tried to use the code but i am getting an error. Please help.
    error is “Could not find file C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\hello.aspx “. I tried to create a page with hello.aspx name.

    • Hi aravind …
      in order to make this code work
      you have to add one aspx page with name DemoPage.aspx then write this code
      this will work
      (steps select sol explorer then Right click on selected project then
      add new item select aspx page name it as DemoPage.aspx)

      thanks and regards
      santosh kakani

  7. Raju says:

    Thanks…great work….

  8. Stephen Atkinson says:

    i copied your tutorial exactly but for some reason when i try replace the DemoPage word it crashes and says it cant find the location

    • @Stephen

      ok then do one thing first rename the Demopage.aspx page in your project then rename it in programe it will run with no error’s

      The reason for this error is in you programe you have created DemoPage.aspx page and you are using it to make dynamic copy of page. so in order to rename Demopage in your Solution Explorer please rename the aspx page like this <>.aspx,<>.aspx.cs then try to replace Demopage word with <> then it will work

      • Stephen Atkinson says:

        Thanks, but i managed to fix it :). it was trying to locate the file in my c drive so i just added the server map path and it works fine 🙂

        FindReplaceFileString(Server.MapPath(“”) & “\\” + TextBox1.Text & “.aspx”, “DemoPage.aspx.vb”, TextBox1.Text + “.aspx.vb”)

      • Stephen Atkinson says:

        also got a delete working for it if anyone needs it xD

        File.Delete(Server.MapPath(“”) & “\\” + TextBox1.Text + “.aspx”)
        File.Delete(Server.MapPath(“”) & “\\” + TextBox1.Text + “.aspx.vb”)

  9. umair ali says:

    Great work

  10. rebai khan says:

    I have two doubt(s)
    i followed the above given steps and created a New aspx(named as MyDynamic.aspx) and its codebehind page dynamically. i understand MyDynamic.aspx page is derived from Demopage.aspx,
    Doubt1: How to remove the code dyamically which was in MyDynamic.aspx and MyDynamic.aspx.vb page .Because the code are (copy & paste from demp.aspx page). i no need that codes in this newly created(MyDynamic.aspx ) page.

    Doubt2: Dynamically How to write a code in the new page (MyDynamic.aspx.vb )?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s