Create code behind file after aspx has been created

ⅰ亾dé卋堺 提交于 2020-01-11 17:25:52

问题


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?


回答1:


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.




回答2:


Based on what I found here: http://forums.asp.net/t/1229894.aspx/1

  1. Right click on your solution explorer.
  2. Add New Item -> Class File.
  3. Name the file as the name of your aspx eg: Default.aspx.cs
  4. When it asks you the file should be in app_code click "no".
  5. In your aspx in page attribute add

    AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"
    
  6. Similarly in your class file that you just added remove everything. Your class should look like this:

    //all namespaces go here
    
    
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
    



回答3:


In Visual Studio 2012: Right click on the project --> click Add --> click Web Form --> Copy the content of your original aspx file into the new WebForm aspx --> delete the original aspx file --> Rename the new one to anything you want. Now you should have a new aspx file with a code behind file that is ready for use




回答4:


After you add the new .cs file, you may want to see the file look like a codebehind file (indented, icon, etc). To do so:

  1. Unload the project
  2. Edit the project
  3. Find the new filename (file.aspx.cs) in the section with files.
  4. Add an xml node for DependentUpon.
  5. Save and Close the project
  6. Reload the project

For a file Profile.aspx.cs, the xml should look something like this:

<Compile Include="Profile.aspx.cs">
  <DependentUpon>Profile.aspx</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Compile>



回答5:


To save yourself from the drama of manually editing the project file like David Frette details, I suggest you remove the file from the project and create a new file with the same name with a code-behind. Then copy-paste the contents of the original aspx or ascx to the new files.



来源:https://stackoverflow.com/questions/9770482/create-code-behind-file-after-aspx-has-been-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!