Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple code behind file for .aspx page

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>
like image 435
Adesh Arote Avatar asked Feb 13 '23 20:02

Adesh Arote


2 Answers

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

like image 103
Alex Barac Avatar answered Feb 16 '23 02:02

Alex Barac


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

like image 28
Thanos Markou Avatar answered Feb 16 '23 04:02

Thanos Markou