Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I requirement is my code Behind file contains more than 5 thousand line, Now i want to split the code into two file ...
The other file also accept the web controls events

i tried with partial class

This is my Code Behind File

C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}



this my class added to App_code Folder

C#
public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
}


here the Button1_Click method is web control
Posted
Comments
Sandeep Mewara 16-Mar-11 8:01am    
And the issue is?
[no name] 16-Mar-11 9:25am    
With 5000 lines you probably need to separate you code into layers not separate files.
Yonghao Chen 17-Mar-11 1:54am    
What about using User Control?
Rajesh Kumar Chekuri 18-Mar-11 2:38am    
my business code separated ...to design
all code is control related

Layers is the one you needed. But if you don't know how to separate you code in the presentation and business layers then in your case you can use hierarchical pages.

For example

this is a class file in the app code. Put all the common codes here. what I shown is wrong, it is not good advice to put event handler here. Common business logic can go here. I just put an event handler here just to show the possibility.

C#
public partial class BasePage : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("button Clicked");
    }
}


Now you can inherit your page from this class.

C#
public partial class _Default : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click +=new EventHandler(Button1_Click);
    }
}



Now the button click is in another file, but can be called from your page code. Instead of using event handlers like this, you can move the common function to a parent class and inherit the pages accordingly.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 13:56pm    
Good points, my 5, but some clarifications on partial may be needed.
--SA
Sergey Alexandrovich Kryukov 16-Mar-11 14:11pm    
Albin, I added my Answer about another feature. Please see. (Did you pay attention is it possible and very beneficial?) Now, the techniques I described fit works very well with the inheritance style you demonstrate in your Answer. Please see and say what do your think. Teamwork? :-)
--SA
Albin Abel 16-Mar-11 14:48pm    
Thanks SAKryukov. This link may give some insight on the code behind partial key word. http://msdn.microsoft.com/en-us/magazine/cc163675.aspx
Espen Harlinn 16-Mar-11 19:20pm    
5ed!
Albin Abel 16-Mar-11 22:59pm    
Thanks Espen Harlinn
This is absolutely correct and good idea to split a class into partial declarations and put them in different parts.

I only want to know that even split class should not too big, so many functional layers could be split not just like partials of the same class, but abstracted as different classes.

This is one important feature which was not clarified here, but is absolutely great for supportability.
You don't need to repeat the list of all base classes/interfaces for each part of the partial! Effectively, the list is merged from all parts of partial declarations. This is a very good feature.

Why this is very important. Consider a Form, for example. You have just one declaration of the base class: like partial class MyForm : Form {/*...*/}. Two partials are generated for you if you start with the proper template, and then you add 1,2,.. N more parts using partial declaration. It is very important of you do not repeat ": Form" in all those files. Why? Because this way, you can change the base class by changing just one word in one file. This is an easy pathway to Form inheritance and otherwise good to modify inheritance.

Even more impressive benefits are brought to interfaces. Imagine you want to implement several interfaces. Do it in different files. As you don't have to repeat all interfaces in the list after ":", you only mention each interface in the file where it is implemented. The benefit is good visual separation of concerns, readability of code and supportability.

—SA
 
Share this answer
 
Comments
Albin Abel 16-Mar-11 14:51pm    
Good suggestion. I appreciate. He needs functional layers. The Interfaces definitely give a loose coupling contract then concrete inheritence. My 5+++
Sergey Alexandrovich Kryukov 16-Mar-11 15:18pm    
Thank you very much. Well, it's still same coupling, same concreteness of interfaces. Just coding ("formatting") style, which is very important in the same way as, say, good naming style/conventions.
--SA
Albin Abel 16-Mar-11 14:55pm    
by the way just now got a funny formula (Nothing related to this question, may be we need to move this to a general forum). humorous stimuli: h = m x s. Where, The pleasure we get (h) is calculated by multiplying the degree of misinformation perceived (m) by the extent to which the individual is susceptible to taking it seriously (s). ScienceDaily (Mar. 15, 2011)
Sergey Alexandrovich Kryukov 16-Mar-11 15:13pm    
Hm. I would say, highly censored :<. See the comment below. :-)
--SA
Sergey Alexandrovich Kryukov 16-Mar-11 15:12pm    
Old! The canonical one was (sorry, I have to replace Unicode with "pi" (greek) and "inf" (math.inf):

Sexual pleasure = N/A, where N: number (of of opposite-sex partners), A: accessibility (the harder the better).
Case one: a secret lover:
N=1/A->0 = inf: infinite pleasure;
Case two: a wife
N=1/A->inf = 0: zero pleasure;
Case tree: same-sex partner:
N=0/A->0: indefinite, needs resolution by L'Hopital's rule, results in pi*D*R ("pederasty")
(N=0, see the definition of N above)
Case four: bordello:
N->inf/A->inf: indefinite, L'Hopital's rule results in 3*pi*R (tripper)

Pretty dumb old joke.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900