Custom Master Page Referencing Controls in Code Behind
-
06-12-2019 - |
Frage
I have built a custom SharePoint 2010 master page where I have dropped a standard asp.net DropDownList control. I also have wired up a code behind file where I want to manipulate that control from.
However for reasons not apparent to me, I am NOT able to reference this control in the code behind.
this.myDropdown
Intellisense does not show it. I cannot even get a handle on it using:
(DropDownList)Page.FindControl("myDropdown")
What am I doing wrong here?
Thanks
Lösung
This works!
(DropDownList)this.Page.Master.FindControl("myDropdown");
Andere Tipps
You should be able to refer your control normally in codebehind class for the master. Check the steps to create a code behind for the master page :
1) Create a class inheriting from System.Web.UI.MasterPage.Declare the control in the class. For e.g.
public class CustomMasterClass : System.Web.UI.MasterPage { protected DropDownList myDropdown; protected void Page_Load(object sender, EventArgs e) { myDropdown.Width = 200; } }
2) Build it into a dll and add to GAC.
3) Add it to the safecontrols list on your web.config.
4) Add the same control to your master page as :
<asp:DropDownList ID="myDropdown" runat="server"/>
5) Add reference to the class in your master. For e.g. :
<%@ Master language="C#" Inherits="CustomNamespace.CustomMasterClass, CustomNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ae015afe5f30fb68" %>