Click here to Skip to main content
11,925,896 members (61,001 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET partial
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

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

this my class added to App_code Folder

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 16-Mar-11 2:20am
Sandeep Mewara 16-Mar-11 8:01am
And the issue is?
Mark Nischalke 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 design
all code is control related
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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.

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.
SAKryukov 16-Mar-11 13:56pm
Good points, my 5, but some clarifications on partial may be needed.
SAKryukov 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? :-)
AlbinAbel 16-Mar-11 14:48pm
Thanks SAKryukov. This link may give some insight on the code behind partial key word.
Espen Harlinn 16-Mar-11 19:20pm
AlbinAbel 16-Mar-11 22:59pm
Thanks Espen Harlinn
Rajesh Kumar Chekuri 18-Mar-11 2:41am
Thanks For reply ...
i have idea about it...
then what is the use partial class it it only for class library
AlbinAbel 18-Mar-11 5:05am
Hi Rajesh, This code behind is a special kind of partial class that is partial with the page. So that you can access any page control in it. It has a code behind attribute which internally visual studio use. If this code behind allowed to be partial with other classes then you will be merging pages together, isn't it. A total cocktail of response and requests. To clean up that mesh there is a restriction implied for code behind not to be partial other than code behind file. It is partial in way any class which inherit page can be a partial to the page class and it access the controls from the page as other partial classes do. Hope you could understand what I am telling.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

AlbinAbel 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+++
SAKryukov 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.
AlbinAbel 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)
SAKryukov 16-Mar-11 15:13pm
Hm. I would say, highly censored :<. See the comment below. :-)
SAKryukov 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.

AlbinAbel 16-Mar-11 15:21pm
:) :)
Espen Harlinn 16-Mar-11 17:46pm
Good advice - 5ed!
SAKryukov 16-Mar-11 18:48pm
Tested on myself and volunteers... :-)
Than you, Espen.
Rajesh Kumar Chekuri 18-Mar-11 2:40am
Thanks For suggestion
SAKryukov 18-Mar-11 2:55am
You're welcome.
Will you formally accept my Answer?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 16 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100