How To Properly Group Partial Class Files in Visual Studio 2005

Posted on July 30, 2006  |  

Posted in Development

11 comments

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.

11 comments

Mahesh Gulati
on August 4, 2006

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.


saket kumar
on November 30, 2006

ASP .NET Resources is really a best site for the developers.

I got my Partial classes solution from this website easily.


Thanks.....


Andrés Felipe Tabares
on September 5, 2007

Somebody, where is .csproj file, I don`t Know

tank for help.....


Milan Negovan
on September 6, 2007

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.


Andres Felipe Tabares
on September 6, 2007

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.....


RaviKirishna
on November 16, 2007

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.


vitta
on March 3, 2008

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


Milan Negovan
on March 3, 2008

Vitta, good point!


P Jones
on March 31, 2008

This works brilliantly with my web application project in vb - using the *.vbproj file of course! Thank you.


pacer
on September 22, 2009

very useful, thanks!


Blake
on October 14, 2009

Great post!

To avoid having to restart Visual Studio each time, you can:
1) Save your project file in VS (critical!), and leave VS running.
2) Make the necessary changes to the project file in Notepad (and save them using Notepad).
3) Switch back to Visual Studio. At this point, VS should detect the changes to your project file and ask you whether you want to reload it. Confirm that you want to reload the project, and voila! Your files are nested appropriately.

CAUTION: Be sure you save your project in VS before making the changes in notepad! Otherwise, when you reload the project in VS, you will lose any project-level changes!