Skip navigation.

How To Make Domain Model PresentableAll recent postsSnapshot vs. Live Collections

How To Properly Group Partial Class Files in Visual Studio 2005

Partial classes have been a very useful addition to the .NET Framework. You’ve seen this before: Visual Studio 2005 places code-behind logic of every Web Form in a partial class. This allows you to split code into several files, each with a public partial class Xxxx declaration to be eventually complied into one class.

Partial classes are extremely helpful when a class is party code–generated and partly written by hand, which is exactly what happens in VS 2K5: it is thanks to a code-gen’ed .designer.cs class that you don’t have to list server controls in code-behind anymore. By hand, that is—it’s done for you automatically.

Adding new partial class files

Partial class (before)What if PageA (shown in the screen shot) code-behind logic grows to a point where you want to split it into two physical files? For example, we want to move some code to PageA_p2.aspx.cs.

If you right-click on PageA.aspx, there’s no option to add a code file. You have to add a new file, PageA_p2.aspx.cs, to the project itself. Open the new code file and replace its declaration with public partial class PageA.

Next, close the project and open the .csproj file (PartialClasses.csproj in this case) in a text editor. Find the following element:

<Compile Include="PageA_p2.aspx.cs">
   <SubType>ASPXCodeBehind</SubType>
</Compile>

and edit it as follows:

<Compile Include="PageA_p2.aspx.cs">
   <SubType>ASPXCodeBehind</SubType>
   <DependentUpon>PageA.aspx</DependentUpon>
</Compile>

Partial classes (after)Save the file, close it and open your project in VS 2K5 again. Your new file should be displayed under PageA.aspx, which is precisely what we were after.

Conclusion

I really don’t know if there’s an easier way. Visual Studio definitely should handle this scenario better.

Comments

Comment permalink 1 Mahesh Gulati |
But VS 2005 doesn't have any csproj file. then how can we bind a separate file with the webpage. Also please clearify me that if I am adding a function in the partial class in different page then other partial class is not picking that function & compile time error comes that function doesn't exist in current context. But if I am putting the partial classes in same page then project get compiled & runs fine.
Comment permalink 2 saket kumar |
ASP .NET Resources is really a best site for the developers.

I got my Partial classes solution from this website easily.


Thanks.....
Comment permalink 3 Andrés Felipe Tabares |
Somebody, where is .csproj file, I don`t Know

tank for help.....
Comment permalink 4 Milan Negovan |
If you create a web site, as opposed to a web application project (WAP), the solution file is in My Documents \ Visual Studio 2005 \ Projects \ [Your site]. I don't think you get a .csproj file with a web site, though.
Comment permalink 5 Andres Felipe Tabares |
Hi, I need to look for .csproj file in my web Aplication because in this document the author explains that I need that file to I create a partial class of my code-behind.

or the mistaken author this about the file that there is to modify?

thanks for help.....
Comment permalink 6 RaviKirishna |
Hi All,

I have one question here..

even though, we have the partial classes aspx, aspx.cs classes for a page; how .net is adding one more partial class to the above classes to generate intialize code(like component initialisation).

And where that code is stored in the system

Thanks,
Ravi.
Comment permalink 7 vitta |
If you are using website model,
you can’t drop a partial class definition for the web form into App_Code, either, because App_Code compiles into a separate assembly, and partial class definitions cannot span multiple assemblies. Web site projects in Visual Studio 2005 gives exactly one code-beside file per web form.

extract from 'The Promise of Partial Classes and ASP.NET'
http://odetocode.com/Blogs/scott/archive/2006/04/20/3380.aspx
Comment permalink 8 Milan Negovan |
Vitta, good point!
Comment permalink 9 P Jones |
This works brilliantly with my web application project in vb - using the *.vbproj file of course! Thank you.

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):