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 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,

share|improve this question
    
There are too many variables. Custom masterpage? Collaboration site/publishing site? Do you need to read controls on the page? –  Paul Schaeflein Mar 28 '12 at 14:49
add comment

3 Answers

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=1.0.0.0,
   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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.