如何在.aspx页的文件后面创建多个代码 [英] How to create multiple code behind file for .aspx page

查看:162
本文介绍了如何在.aspx页的文件后面创建多个代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我需要在c#asp.net中的单个.aspx页面的文件后面创建多个代码。实际上,我有一个完成大量编码的Web表单,并且我需要多个开发人员同时对其进行处理。我怎么能达到相同的目的?

I have a situation where I need to create multiple code behind files for a single .aspx page in c# asp.net. Actually I have a web form on that huge coding is done and I need multiple developer's working on it simultaneously. How can I achieve the same?

这是我尝试过的代码段

Class 1 MyPartialClass.cs

Class 1 MyPartialClass.cs

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void PrintText(string pt)
        {
            Response.Write(pt);
            //lblTest.Text = pt;    //Can not access this label in partial class.
        }
    }
}

Class 2(默认)。 aspx.cs

Class 2 which is Default.aspx.cs

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PrintText("Hello World");
        }
    }
}

和我的HTML源代码

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblTest" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>


推荐答案

ASP.NET始终会在文件后面生成代码,如下所示:局部类

ASP.NET will always generate your code behind files as partial classes

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

因此,如果您将定义与 partial 并将类保留在相同的名称空间下。

You can therefor separate the code behind in different files, if you pertain the definition as partial and keep the class under the same namespace.

编辑: Web项目

//Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"     AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Content>

//Default.aspx.cs
namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PrintText("Hello World");
        }
    }
}

//MyPartialClass.cs
namespace WebApplication1
{
    public partial class Default
    {
        protected void PrintText(string pt)
        {
            Response.Write(pt);
            lblTest.Text = pt;    //lblTest is accessible here
        }
    }
}

我没有修改任何其他生成的文件。我想提到的一件事是,生成的 Default.aspx.cs 文件是使用类名 _Default生成的。我将其更改为 Default ,Visual Studio在包含该类定义的所有文件中重构了更改,但 Default.aspx 文件中,我必须手动将其从 Inherits = WebApplication1._Default 修改为 Inherits = WebApplication1.Default

I haven't modified any other generated files. One thing I like to mention is that the Default.aspx.cs file that has been generated was generated with the class name "_Default". I have changed it to Default and Visual Studio refactored the change across all files that contain a definition for that class, except the Default.aspx file, where I had to manually modifiy from Inherits="WebApplication1._Default" to Inherits="WebApplication1.Default".

编辑2:

我一直在搜索互联网,并根据 http://codeverge.com /asp.net.web-forms/partial-classes-for-code-behind/371053 ,您尝试执行的操作是不可能的。 http中详细介绍了相同的想法: //codeverge.com/asp.net.web-forms/using-partial-classes-to-have-multiple-code/377575
如果可能,请考虑从网站转换为Web应用程序支持您要实现的目标。这是有关如何执行此转换的演练: http://msdn.microsoft.com/zh-CN/library/vstudio/aa983476(v = vs.100).aspx

I kept searching the internet, and according to http://codeverge.com/asp.net.web-forms/partial-classes-for-code-behind/371053, what you are trying to do is impossible. Same idea is detailed at http://codeverge.com/asp.net.web-forms/using-partial-classes-to-have-multiple-code/377575 If possible, consider converting from Web Site to Web Application, which supports what you are trying to achieve. Here is a walkthrough on how to perform this conversion: http://msdn.microsoft.com/en-us/library/vstudio/aa983476(v=vs.100).aspx

这篇关于如何在.aspx页的文件后面创建多个代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆