创建aspx后,在文件后面创建代码

时间:2023-01-20 21:16:36

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?

我刚刚继承了一个由设计师创建的网站。该网站最初是使用所有* .html文件创建的。设计者将所有* .html文件重命名为* .aspx文件。因此,没有创建aspx.cs文件。我将网站推向了新的VS2012解决方案。我的问题是,VS 2010中是否有一种方法可以自动为现有的独立aspx文件创建文件背后的代码?

4 个解决方案

#1


19  

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:

我不知道这样做的自动方式,但如果现有的* .aspx文件中没有服务器端代码,那么它应该只是添加.cs代码隐藏文件然后在< %@ Page标签如下:

<%@ 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.)

注意:这不会创建YourPage.aspx.designer.cs文件。 (我通常删除它们,因为它们会导致合并问题 - 我发现添加控件我需要手动引用我的代码隐藏文件更容易。)

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.

另一种方法是为每个页面创建一个具有正确名称的新“Web表单”,然后将现有标记复制并粘贴到其中。如果您在现有* .aspx文件中有服务器代码,则需要手动将其复制到代码隐藏。

#2


5  

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

根据我在这里找到的内容:http://forums.asp.net/t/1229894.aspx/1

  1. Right click on your solution explorer.
  2. 右键单击解决方案资源管理器。
  3. Add New Item -> Class File.
  4. 添加新项目 - >类文件。
  5. Name the file as the name of your aspx eg: Default.aspx.cs
  6. 将文件命名为aspx的名称,例如:Default.aspx.cs
  7. When it asks you the file should be in app_code click "no".
  8. 当它询问您文件应该在app_code中时,单击“否”。
  9. In your aspx in page attribute add

    在你的aspx in page属性中添加

    AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"
    
  10. 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


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

在Visual Studio 2012中:右键单击项目 - >单击添加 - >单击Web窗体 - >将原始aspx文件的内容复制到新的WebForm aspx中 - >删除原始的aspx文件 - >重命名任何你想要的新东西。现在你应该有一个新的aspx文件,其中包含一个可以使用的代码隐藏文件

#4


1  

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:

添加新的.cs文件后,您可能希望看到该文件看起来像代码隐藏文件(缩进,图标等)。为此:

  1. Unload the project
  2. 卸载项目
  3. Edit the project
  4. 编辑项目
  5. Find the new filename (file.aspx.cs) in the section with files.
  6. 在包含文件的部分中找到新文件名(file.aspx.cs)。
  7. Add an xml node for DependentUpon.
  8. 为DependentUpon添加xml节点。
  9. Save and Close the project
  10. 保存并关闭项目
  11. Reload the project
  12. 重新加载项目

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

对于文件Profile.aspx.cs,xml应如下所示:

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

#1


19  

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:

我不知道这样做的自动方式,但如果现有的* .aspx文件中没有服务器端代码,那么它应该只是添加.cs代码隐藏文件然后在< %@ Page标签如下:

<%@ 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.)

注意:这不会创建YourPage.aspx.designer.cs文件。 (我通常删除它们,因为它们会导致合并问题 - 我发现添加控件我需要手动引用我的代码隐藏文件更容易。)

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.

另一种方法是为每个页面创建一个具有正确名称的新“Web表单”,然后将现有标记复制并粘贴到其中。如果您在现有* .aspx文件中有服务器代码,则需要手动将其复制到代码隐藏。

#2


5  

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

根据我在这里找到的内容:http://forums.asp.net/t/1229894.aspx/1

  1. Right click on your solution explorer.
  2. 右键单击解决方案资源管理器。
  3. Add New Item -> Class File.
  4. 添加新项目 - >类文件。
  5. Name the file as the name of your aspx eg: Default.aspx.cs
  6. 将文件命名为aspx的名称,例如:Default.aspx.cs
  7. When it asks you the file should be in app_code click "no".
  8. 当它询问您文件应该在app_code中时,单击“否”。
  9. In your aspx in page attribute add

    在你的aspx in page属性中添加

    AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"
    
  10. 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


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

在Visual Studio 2012中:右键单击项目 - >单击添加 - >单击Web窗体 - >将原始aspx文件的内容复制到新的WebForm aspx中 - >删除原始的aspx文件 - >重命名任何你想要的新东西。现在你应该有一个新的aspx文件,其中包含一个可以使用的代码隐藏文件

#4


1  

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:

添加新的.cs文件后,您可能希望看到该文件看起来像代码隐藏文件(缩进,图标等)。为此:

  1. Unload the project
  2. 卸载项目
  3. Edit the project
  4. 编辑项目
  5. Find the new filename (file.aspx.cs) in the section with files.
  6. 在包含文件的部分中找到新文件名(file.aspx.cs)。
  7. Add an xml node for DependentUpon.
  8. 为DependentUpon添加xml节点。
  9. Save and Close the project
  10. 保存并关闭项目
  11. Reload the project
  12. 重新加载项目

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

对于文件Profile.aspx.cs,xml应如下所示:

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