making multiple c# file for one asp.net page
For my project(in asp.net) i wrote near 1000 lines of c# code for one asp.net page.It includes so many functi开发者_JS百科ons.The problem is,it is going complicated while i am writing more codes on one page.How can i make multiple c# files for one asp.net page?? I tried by adding new class in VS2008.But calling a function from one file to other is making error(item is not present in current file).How can i do that??
Partial classes will allow you to split your large class over many files. However, I would suggest that is not the ideal solution.
You could create other classes that your page uses to perform a lot of its functionality.
You could move some of the functionality into UserControls.
You could move some shared functionality into a master page and then have multiple pages to perform individual tasks.
Use partial class.
Although you can move some of your code into a separate partial class
file, more likely a page that long means you should move some of your functions out of the codebehind (.aspx.cs) file into a separate code (.cs) file.
There must be some code on that page you'll find handy on others.
The simple answer is to use Partial Classes.
If you have code in another file just give it a namespace and include it in the original page.
I.e.
Newclass.cs
namespace myapp.functions
{
//code to include.
}
Page.aspx.cs
using myapp.functions;
// now you can use them
While I see why everyone is suggesting using partial classes, they only provide physical separation of code in separate files. I think your problem here is not the separation of the code itself, but rather splitting your functionality in components.
If there's functionality in a page that is logically grouped together, why not extract it in an ASP.NET control, expose events in this control and have the code + functionality grouped logically? This would add a bit of more effort required, but would be much, much cleaner and easily maintained. Also, if you decide to reuse some part of the page, it would be copy-paste hell if the functionality is contained as code in a page and not a control.
精彩评论