Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create code behind file after aspx has been created

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?

like image 348
Mike Murphy Avatar asked Mar 19 '12 12:03

Mike Murphy


People also ask

How do you relate an ASPX page with its code behind page?

Right-click the . aspx page, and then click View Code. The code-behind file opens in the editor. Switch from the code-behind class file to the .

Does ASPX need to be compiled?

In order for the ASP.NET engine to service a request for this page, the page's code portion (the WebPage. aspx. cs file) must first be compiled. This compilation can happen explicitly or automatically.

Can multiple ASPX pages share the same code file?

Yes. It is technically possible, but it is not a supported way of using ASP.NET and there will likely be gnarly difficulties with it.


1 Answers

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.

like image 161
Robbie Avatar answered Oct 12 '22 09:10

Robbie