I have a .aspx Web Form with a .aspx.cs code-behind. The code-behind is nearly 2000 lines long, and it's getting to the point where the only way to easily navigate it is to put a ton of spaces in between the sections, zoom out so I can see the physical look of the code, and then zoom in where I want to edit. In other words, it's a major pain. I'd like to split this 2000 line into different files that represent concepts in the code. So when I need to go edit some functionality in the "Employee" jquery tab of the page, I can just go to the partial class that contains just the "Employee" functionality.
In the New Item menu, I cannot find anything for an additional code-behind file, no .aspx.cs file. I tried creating a .cs file, renaming it to .aspx.cs, and giving it the same partial class name --- no go, I couldn't see any of the methods of the default partial class, nor the controls on the page.
I understand the concept that if your "class", in the OOP sense, is that long, it's doing too much. What I don't understand is a "class" in terms of code-behind for a web form. The form can't really be broken down into smaller forms --- the user experience needs to all take place on one page.
You could use partial classes. So add a new
.cs
file to your project and use a partial class.For example assuming you currently have the following
Default.aspx.cs
file:you could add another .cs file to your project in which:
It's very important that this partial class is declared in exactly the same namespace as the original class and has the same name.
But note that using partial classes only hides the shit. It doesn't clean it. If you want to clean it you will have to consider OOP practices to split functionality in different classes. You should layer your application.
Try;
followed by
The use or partial classes will solve the generic problem that you are looking into. There is another way that you can manage your code by using region tags within your code, so that you can open and close regions of code.
Example:
This will allow you to mark up your code to track through it better.
Hope that helps
It's possible to split your classes into separate files using partial classes - see
http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx
However, 2000 lines does seem fairly large for your code behind. Have you considered extracting functionality into different helper / strategy classes which are tailored to specific functions? So instead of creating a new partial class for "Employee" functionality, create a new class with testable helper and logic functions, and add a reference to these from your original class?