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 have a page layout that I would like to include a listview that is populated from the code behind. I tried right clicking on my aspx file and choosing "View Code" like I typically would but it does not take me to a code behind as I would expect.

Is it possible to have a code behind file for my Page Layout? Should I just try add code sections to my aspx page?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Yes, it is possible to add code behind to Page Layouts. You can follow this link.

I would not suggest adding code directly on the .aspx pages.

A very common alternative to avoid code behind files is to add controls/web parts to the page layout and let all your code be in those custom control/web parts.

share|improve this answer
    
I had considered adding webparts to the page, but if I have the page layout in 10 different places wouldn't I have to add the web part 10 times? –  Abe Miessler Nov 18 '11 at 19:24
    
yes, to a large extent that's true but there are ways to provision web parts to page layout when a page is created using that page layout. but if web part must exist on all pages created from that page layout, I would consider wrapping that web part around a cotrol and place the control directly on the page. –  Ashish Patel Nov 18 '11 at 19:28

Due to ghosted pages in SP database it's strongly recommended not to have code on pages, cause when someone modify the page layout using SPD it will be compiled in separate assembly to one in feature folder and cause no code allowed, it'll break to save and compile. Thus you can use either solution from @Ashish or just place required controls to page layout as list view and custom data source using DataSourceID propery.

share|improve this answer

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.