Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to make duplicate copies of existing ASPX page in Visual Studio

What is the best way to create a copy of an existing ASPX page in a web project in Visual Studio?

If I right click on the ASPX file in the Solution Explorer and select copy, then paste, it looks like what I was expecting. I get a new copy of the ASPX file and the code behind files. I can then rename the ASPX file and the code behind files get renamed as well.

Looking good, until I open the ASPX file. Then I see that it has the same "Inherits" tag that the original file had, and it points to the orginal class name.

Is there something that I am missing? I realize I can mannually edit this, and the code behind files (since they are declaring the same class name, which causes build errors). Is there a better way to do this, or should I just man up and start doing some manual work? :-)

like image 697
Richard West Avatar asked Apr 04 '11 20:04

Richard West


People also ask

How do I copy source code in Visual Studio?

Go to Tools -> Options. type copy in the search box. Under Text Editor -> Advanced ... Check Copy rich text on copy/cut.

How do I copy a web form?

Find the Web Form you want to duplicate (clone) in the list and click the grey 'Duplicate' button. A dialog box will appear asking you to confirm that you wish to duplicate - click OK. Your Web Form is now duplicated (cloned) as 'Copy of x', x being the name of the form.


4 Answers

I almost always create a brand new page, then just copy the "guts" (leaving page directive info, namespaces, etc) followed shortly by some find and replace if needed.

like image 100
Mark B Avatar answered Sep 19 '22 05:09

Mark B


The following works in VS 2013

  1. Copy and paste the desired aspx page using VS right click copy\paste.
  2. Rename aspx page using right click 'rename'.
  3. Change Inherits tag at top of aspx page to the new name. (be aware - not all characters allowed).
  4. View Code.
  5. Change name of Class to match the Inherits tag in step 3.
like image 25
Brian Avatar answered Sep 21 '22 05:09

Brian


I think Resharper has a 'Duplicate' refactoring that works on ASPX's as well. The default shortcut should be Ctrl-D

like image 34
sehe Avatar answered Sep 20 '22 05:09

sehe


I figured it out - so no need for an answer, but I am leaving this here as I think others may find it useful.

What I did is to open one of the aspx page aspx.designer.vb files (they are all identical for my identical code-behind files) and copy the auto-generated WithEvents declarations for the controls.

I then turned the aspx.vb and aspx.designer.vb files into blank files (when I deleted them, the compiler complained that it couldn't find them - not sure yet how to fix that). Magic! All my aspx files now point to my new class (and file) and I no longer have the duplicated code.

By the way, this is not just a case of using multi-tier design. I already had all the substantive code in one place, but I still had to duplicate the calls from Page_Load and various control click functions into that code. Now I have nothing duplicated...

Brian

like image 42
Brian Weiss Avatar answered Sep 18 '22 05:09

Brian Weiss