Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
add comment

4 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
1. "Is it just one default.aspx file" -- it is just one default.aspx for a special site of publishing portal. –  George2 Nov 26 '09 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 Nov 26 '09 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. –  Steve Lineberry Nov 26 '09 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 Nov 26 '09 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. –  Steve Lineberry Nov 26 '09 at 21:31
show 1 more comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

http://sharepoint-charles.blogspot.com - Masterpage with codebehind

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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