2

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?

2 Answers 2

4

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.

2
  • 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? Commented Nov 18, 2011 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. Commented Nov 18, 2011 at 19:28
0

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.

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.