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 n开发者_开发知识库ame, which causes build errors). Is there a better way to do this, or should I just man up and start doing some manual work? :-)
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
- Copy and paste the desired aspx page using VS right click copy\paste.
- Rename aspx page using right click 'rename'.
- Change Inherits tag at top of aspx page to the new name. (be aware - not all characters allowed).
- View Code.
- Change name of Class to match the Inherits tag in step 3.
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
Similar to Brian's answer.
In VS2013.
- Highlight the .aspx you want to copy
- Click CTRL-C, then CTRL-V
You'll see a new aspx created with the same with " - Copy.aspx" at the end
- Highlight the new .aspx
- Right-mouse click and select "Rename..."
Type what you want the new .aspx called. Inherits, etc. are updated automatically.
i allready try it and it faild.... so i copy every paragraph on the aspx page alone and paste it in the right place
精彩评论