LinkedIn

Thursday, April 14, 2011

SharePoint 2010 custom masterpage with code behind file

Master Page code behind should like this:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MasterPageWithCodeBehind.MasterPageModule

{

public class _starter : MasterPage

{

protected System.Web.UI.HtmlControls.HtmlGenericControl divRibbonContainer;
protected Label Label1;

protected void Page_Load(object sender, EventArgs e)
{

     divRibbonContainer.Visible = false;
     Label1.Text = "Hello World!";
}

}

}
 
In the application pages you should have a attribute like this:
Add the following Inherits attribute:
Inherits :
To combine the code-behind file with the masterpage there need to be an attribute added to the masterpage directive.
The following data is needed:
■Namespace of the class & Type/Class name (these need to be seperated by a dot) (MasterPageWithCodeBehind.MasterPageModule._starter)

■Strongname/Assembly in my case was this the same as the projectname (MasterPageWithCodeBehind)

■Version (Version=1.0.0.0)

■Culture (Culture=neutral)

■PublicKeyToken (PublicKeyToken=f8a88530fbc7b81b)In the masterpage navigate to the following:

In our case the Inherits would contain:
MasterPageWithCodeBehind.MasterPageModule._starter, MasterPageWithCodeBehind, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f8a88530fbc7b81b
 
Deploy your SharePoint 2010 project and load the default site.Your ribbon should be gone and a text like Hello World! should be visible.
Refer the following link for details:
http://rburgundy.wordpress.com/2010/03/10/sharepoint-2010-custom-masterpage-with-code-behind-file-%e2%80%93-part-2/

No comments:

Post a Comment