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? :-)
Go to Tools -> Options. type copy in the search box. Under Text Editor -> Advanced ... Check Copy rich text on copy/cut.
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.
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.
The following works in VS 2013
I think Resharper has a 'Duplicate' refactoring that works on ASPX's as well. The default shortcut should be Ctrl-D
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With