How to create multiple code behind file for .aspx page

六月ゝ 毕业季﹏ 提交于 2020-01-02 06:46:15

问题


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?

Here is the code snippet I tried

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 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");
        }
    }
}

and my HTML source

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

回答1:


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)
        {

        }
    }
}

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.

Edit : The Web Project

//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
        }
    }
}

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".

Edit 2:

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




回答2:


You need to set up a source safe server in order to that, like Team Foundation Server.




回答3:


You can easily just add partial classes to your code.

http://www.codeproject.com/Articles/709273/Partial-Classes-in-Csharp-With-Real-Example



来源:https://stackoverflow.com/questions/23996069/how-to-create-multiple-code-behind-file-for-aspx-page

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