Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top