Click here to Skip to main content
Rate this: bad
good
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
Comments
Sandeep Mewara at 16-Mar-11 8:01am
   
And the issue is?
Mark Nischalke at 16-Mar-11 9:25am
   
With 5000 lines you probably need to separate you code into layers not separate files.
Yonghao Chen at 17-Mar-11 1:54am
   
What about using User Control?
Rajesh Kumar Chekuri at 18-Mar-11 2:38am
   
my business code separated ...to design
all code is control related
Rate this: bad
good
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.
  Permalink  
Comments
SAKryukov at 16-Mar-11 13:56pm
   
Good points, my 5, but some clarifications on partial may be needed.
--SA
SAKryukov at 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
AlbinAbel at 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 at 16-Mar-11 19:20pm
   
5ed!
AlbinAbel at 16-Mar-11 22:59pm
   
Thanks Espen Harlinn
Rajesh Kumar Chekuri at 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 at 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
good
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.
 
—SA
  Permalink  
Comments
AlbinAbel at 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 at 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
AlbinAbel at 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 at 16-Mar-11 15:13pm
   
Hm. I would say, highly censored :<. See the comment below. :-)
--SA
SAKryukov at 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
AlbinAbel at 16-Mar-11 15:21pm
   
:) :)
Espen Harlinn at 16-Mar-11 17:46pm
   
Good advice - 5ed!
SAKryukov at 16-Mar-11 18:48pm
   
Tested on myself and volunteers... :-)
Than you, Espen.
--SA
Rajesh Kumar Chekuri at 18-Mar-11 2:40am
   
Thanks For suggestion
SAKryukov at 18-Mar-11 2:55am
   
You're welcome.
Will you formally accept my Answer?
--SA

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

  Print Answers RSS
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 Mar 2011
Copyright © CodeProject, 1999-2014
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