Create code behind file after aspx has been created

asp.netvisual studio 2010

I just inherited a web site that was created by a designer. The site was originally created with all *.html files. The designer renamed all the *.html files to *.aspx files. Hence there are no aspx.cs files created. I pulled the site into a new VS2012 solution. My question is, is there a way in VS 2010 to automatically create the code behind files for a an existing stand alone aspx file?

Best Answer

I don't know of an automated way to do this, but if there is no server side code in the existing *.aspx files then it should just be a case of adding the .cs codebehind files and then wiring them up in the <%@ Page tag like so:

<%@ Page Title="YourPageTitle" Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="YourNamespace.YourPage" %>

Note: This will not create the YourPage.aspx.designer.cs file. (I usually delete these anyway as they cause merge issues - i find it easier to add the controls i need to reference to my code-behind file manually.)

The other alternative is to just create a new "Web Form" for each page with the correct names and then copy and paste the existing markup into them. If you do have server code in the existing *.aspx files then you will need to manually copy it to the code-behind.