I am using SharePiont Server 2007 Enterprise with Windows Server 2008 Enterprise. I am developing using VSTS 2008 + C# + .Net 3.5. I want to add some C# code to default.aspx (more details, adding some logics to Page_Load method of default.aspx) and its code behind file. But I did not find default.aspx and its code behind file on disk? Where are they and how to modify?

BTW: another solution is to create a default.aspx file and its related code behind file by myself? But in this situation how to borrow the existing content/UI from current default.aspx (I am satisfied with current content/UI of default.aspx)?

I am not sure which solution is better and easier in my requirement to add C# code to Page_Load. Any advice or guides?

thanks in advance!

4 Answers 4


Is it just one default.aspx file or do you want to do this with all of them?

If it's all of them then maybe a httpmodule might be a better idea. You can tap into the page load event on any page based on whatever rules you need.

If it's just one of them, then you'll need to compile a dll with a class that inherits from Microsoft.SharePoint.WebPartPages.WebPartPage. Then on your default.aspx, edit it using SP Designer and then change the page directive to inherit from your class.

  • 1. "Is it just one default.aspx file" -- it is just one default.aspx for a special site of publishing portal.
    – George2
    Commented Nov 26, 2009 at 15:37
  • "then you'll need to compile a dll with a class that inherits from Microsoft.SharePoint.WebPartPages.WebPartPage. Then on your default.aspx, edit it using SP Designer and then change the page directive to inherit from your class." -- good idea! Seems just what I want to do. But I never did this before, any recommended documents of this topic for a beginner?
    – George2
    Commented Nov 26, 2009 at 15:37
  • 1
    If it's a publishing page then you might need to inherit from a different baseclass. You have to remember that SharePoint is essentially an ASP.NET app. So all of your normal ASP.NET concepts apply. Also, since this is a publishing page, you won't be able to edit the page directive using sp designer. I think an httpmodule might work out better. Check out: thelineberrys.com/default-category/… for an example of how to write an httpmodule that adds itselt to the page.load event. Commented Nov 26, 2009 at 16:34
  • Steve, actuallly I only need to change code logics in default.aspx (in its Page_Load method). I do not need to touch any other pages. In my requirement, I think modify default.aspx is better than develop a module which will be triggerted for all kinds of requests to my SharePoint site, correct?
    – George2
    Commented Nov 26, 2009 at 17:37
  • 1
    Yea maybe so. You'll just have to do what I said before but apply it to a page layout instead of a webpart page if the page you are reffering to is a publishing page. I've also done some testing with httpmodules and I haven't see any performance difference when using a simple string compare of the request.url. I do agree it's probably overkill though. Commented Nov 26, 2009 at 21:31

Changing content in a single page could be done in many ways. For code changes in several pages http modules would not be my first choice (it would probably be one of my last due to performance depending on what needs to be done).

Much more elegant (and less intrusive) methods exists, such as code behind in master page or page layout or inheriting from publishing page.

For changing a single page you could either simply insert a custom control on an existing page, create a site page or even create an application page.

As Steve say, theres many approaches. The solution you choose should be chosen based on what you more specifically is trying to do, and since you dont give us any specifics it is hard to give good advice on how to proceed.

Try for a second to forget how you technically would solve this in a "normal" asp.net application and tell us what you want to achieve more specifically. Often SharePoint development is special and a shortcut (like using SharePoint Designer) can easily get you in trouble later on.

Also in general you need to think in how to deploy your changes. It sounds a bit to me (also on other questions you ask) like you deploy these changes directly on your production server (i might be wrong, but thats the impression i get).

SharePoint has ways of deploying the changes you talk about here (for example creating a new master page with code behind) using Features and Solution packages.

This not only helps you when you want to move your code from environment to environment (eg. from dev test to integration test to pre-prod to production) but also secures your web front end servers are in sync, and also gives you a way to recreate your site if your harddisks should die on you and your backup doesnt work and you forgot to backup your transaction logs (trust me it will happen some day).


I wrote a post on how to create a simple page with code behind for SharePoint; check it out at: The simplest form of a SharePoint application - part 2

Good luck


you should look at this great tutorial on how to create codebehind to masterpage in visual studio. you even dont need the wsp builder extension. It could be done in very low programming way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.