I have a button that I've added to our masterpage in Sharepoint Designer. What is the best way to write c# code to that button? I'd like to do this in visual studio if possible. Thanks,

  • There are too many variables. Custom masterpage? Collaboration site/publishing site? Do you need to read controls on the page? Mar 28, 2012 at 14:49

3 Answers 3


There are two ways you can consider:

1) If you want to directly access other controls on the master page in the *button_click* event handler, you should go for creating code behind for master page. You have to create a class and compile it to a dll, deploy to GAC.Then you can apply it to master page using below directive:

<%@ Master language="C#" Inherits="YourNamespace.YourMasterCodeBehindClass,
   YourNamespace, Version=,
   Culture=neutral, PublicKeyToken=[assemblykey]" %>

2) If the functionality in the button is not dependent on other controls, You can just create a custom control with button and place it on master page after registering the tagprefix.


The main problem is control deployement. Basically, user control contains to parts - ascx fire and assembly. If you would like to use this control, first at all, you should place ascx file to the _ControlTemplates folder and assembly to the GAC, then reset iis. Thus I think that the best way is to use package for control deployment. In this case the easiest approach is to use Visual Studio that builds this package automatically.


You will need to make a few key decisions before selecting the strategy for button implementation and master page customization. Are you a farm admin? Do you control all site that you are targeting the master page for?
Here is a quick link for sandbox solution based master page customizations.
You will most likely have to develop a strategy to customize a master page in VS2010 and deploy it (either sandbox or farm solutions). Either way i'd suggest to build a custom user control to handle the button rendering and event handling. As long as control is deployed to the same site/site collection, you should be able to include it on a master page markup. Let me know if you need some examples.

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.