Answered by:
Creating a Code Behind File for Default.aspx Page in SharePoint

Question
-
Dear All
I am trying to create a code behind file for my Default.aspx page in SharePoint in order to do some things on the Page_Load event. I have not found definitive instructions to help me do this. I have only been able to find instructions for creating a code behind file for a master page which is not what I am looking to do here.
Please does anyone have a simple set of instructions for adding a code behind file for a Default.aspx page in SharePoint? I understand I have to do this via a Feature but I can't find the steps that I need to follow.
Many thanks for your help
Daniel
- Edited by djs25uk Tuesday, May 3, 2016 8:28 PM
Tuesday, May 3, 2016 8:24 PM
Answers
-
Hi Daniel,
To create a code behind file for SharePoint page, you need to create your own class and set it to inherit from System.Web.UI.Page.
After that, you need to modify your page to inherit from the custom class.
For more details, please check the links below:
http://avinashkt.blogspot.sg/2009/06/how-to-write-server-side-c-code-in.html
http://stackoverflow.com/questions/7112787/add-general-c-sharp-code-to-a-sharepoint-2010-site-page
http://sharepoint.stackexchange.com/questions/139029/how-to-add-code-behind-file-for-home-aspx
Thanks,
VictoriaTechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.- Proposed as answer by Wendy DZMicrosoft contingent staff Tuesday, May 10, 2016 12:39 PM
- Marked as answer by Wendy DZMicrosoft contingent staff Thursday, May 19, 2016 2:58 AM
Wednesday, May 4, 2016 7:53 AM
All replies
-
Daniel—
Probably you can add custom web part and use its OnLoad event.
You can also create custom application pages, which resides with in the _layouts folder. (These pages cannot be set as site welcome page)
---
Rajesh
rjesh.com| @rjesh
You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.Tuesday, May 3, 2016 8:38 PM -
Daniel,
If you try to put some code in the sharepoints page you need to make it in javascript. The other alternative is make it in visual studio.
https://msdn.microsoft.com/en-us/library/ee231557.aspx?f=255&MSPPError=-2147217396
Creo que hablas castellano por tu nombre por lo que si necesitas que siga en castellano avísame.
Por favor recuerde "Marcar como respuesta" las respuestas que hayan resuelto su problema, es una forma común de reconocer a aquellos que han ayudado, y hace que sea más fácil para los otros visitantes encontrar la solución más tarde. Microsoft ofrece este servicio de forma gratuita, con la finalidad de ayudar a los usuarios y la ampliación de la base de datos de conocimientos relacionados con los productos y tecnologías de Microsoft. Este contenido es proporcionado "tal cual" y no implica ninguna responsabilidad de parte de Microsoft.
Tuesday, May 3, 2016 9:49 PM -
you can add a webpart to the default page
or you can develop an application page to replace the default one.
顺其自然地勇往直前!—Justin Liu
Wednesday, May 4, 2016 1:49 AM -
Many thanks for your reply.
I should have said, we have developed many webparts but this isn't really suitable for a webpart. I want to do some things on the actual page like set a class on the body tag.
I don't want to use JavaScript in case it is not enabled on a client's device.
I want to be able to interact with the Default.aspx through a code-behind file - I'm not sure if that rules out the custom application pages.
Thanks
Dan
Wednesday, May 4, 2016 7:04 AM -
Hi Daniel,
To create a code behind file for SharePoint page, you need to create your own class and set it to inherit from System.Web.UI.Page.
After that, you need to modify your page to inherit from the custom class.
For more details, please check the links below:
http://avinashkt.blogspot.sg/2009/06/how-to-write-server-side-c-code-in.html
http://stackoverflow.com/questions/7112787/add-general-c-sharp-code-to-a-sharepoint-2010-site-page
http://sharepoint.stackexchange.com/questions/139029/how-to-add-code-behind-file-for-home-aspx
Thanks,
VictoriaTechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.- Proposed as answer by Wendy DZMicrosoft contingent staff Tuesday, May 10, 2016 12:39 PM
- Marked as answer by Wendy DZMicrosoft contingent staff Thursday, May 19, 2016 2:58 AM
Wednesday, May 4, 2016 7:53 AM