How can I access the property a control on a master page from the master page's code behind.

On my master page I have a label. I want to change this label's text property based on an element of the querystring. (label.text = "NewValue")

All child pages using this master will display the same thing so I was trying to put the code in the master page so as not to have to duplicate it for the child pages or put it in a base class and use FindControl.

Is it possible to access from a master page's codebehind the property of a server control that is placed on a master page's aspx? If so how?
(I saw similar questions, but they all came from the view of a child accessing the property.)

Thank you,

Greg
 

0
greghsr
12/28/2006 8:01:45 PM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

4 Replies
990 Views

Similar Articles

[PageSpeed] 1

It works just like any other page with controls on them. You can access them directly from the master page code behind.
0
Rossoneri
12/28/2006 9:04:39 PM

Accessing directly, in the manner described below, did not work for me. 

If I drag a textbox control (TextBox1) onto the MasterPage.aspx and in the codebehind reference the text box in this manner "TextBox1.Text = "Hello World", despite code completion indicating the presence of the control, when I compile the webproject I get the error "Error    4    The name 'TextBox1' does not exist in the current context".

If I refer to the textbox in this manner "this.TextBox1.Text = "Hello World" on compile I get the error "Error    5    'MasterPage' does not contain a definition for 'TextBox1' "   

I am using a Web Application Project (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/WAP.asp)  

0
greghsr
12/29/2006 4:15:11 PM

Can you post some code so we can see what you're doing, it's hard to tell from the thread so far.

Thanks

0
jasonlindo
12/29/2006 8:46:09 PM
Problem Solved: I messed up. A reserved word was used as the name for the MasterPage. While this did not cause a compile error. It did interfere with functionality.
0
greghsr
12/31/2006 9:02:12 AM
Reply:

Similar Artilces:

How can I access Master page properties in a user control's code-behind?
I have a  page that uses a master page, and also has a user control on it, I would like to acces the master page class properties from code-behind in user control. Ho could I do this? Note I am NOT asking how to access Master page properties in page's code behind but a user control's code behind.sun21170 UserControl has the Page property. In Page property, you can to access the MasterPage. Is it posible you coul dgive a code example .... could use this functionality myself ... many thanks I just found the answer to my question. The answer is as below and it's in ...

Accessing contnet page's control in the Javascript code of master page
Hello  I have one master page in which i put javascript code, CSS and one Content place Holder. In my content page i add two control one for Login and another for pass word and one Custom validator control. Now i want to call the Javascript code which is written in Master page as the Clinet function in my Custom Validator control to run to validate the value entered in the login and password(TextBox) control. How i do this. My content page having Server side script that i want to call to validate page at the server side. For submitting the page i use Button control and in its PostB...

Accessing controls from the master page's code behind
When I select an item in the TreeView on my master page the navigateurl property on the selected node takes the user to a new page. When the page loads for some reason, all nodes in my treeview are expanded even though I set ExpandDepth="1" on the treeview. So since that wouldn't work, I tried to use the OnSelectedNodeChanged method. In the code behind of my master I tried to access the treeview but when I try to compile I get: "Object reference not set to an instance of an object.". I know I am typing the treeview's id right because intellisense is picking it up. Here is some pertinent...

How to set a Master Page's control's property through content page on a postback event?
Hi all, i m using a master page in my application. i have a panel on master page and a tab container with three tab panels on content page. i want to set/change  the visibility of the panel control when the tab is changed on the content page. By default the panel is invisible. To implement this i executed the following steps: 1. Set AutoPostBack of tacontainer to true 2. Wrote an event handler for ActiveTabIndexChanged event of the tab container 3 Wrote the following code on Tab changed event   if (TabContainerEmpPerformance.ActiveTabIndex == 1) { ...

CS0012 when referencing controls on master page's master page.
 I'm working with a content page that has a master page (we'll call it ChildMaster). ChildMaster also has a master page (ParentMaster). There is a control on ParentMaster I need to access from the content page. The code looks like this:  ControlType controlInstance = (ControlType) Master.Master.FindControl("controlID");  When I upload, I get CS0012 telling me that ParentMaster is defined in an assembly that is not referenced, and I need to add a reference to some assembly with a funky random-looking name (which changes when I reupload ParentMaster). Up t...

How to access a Master's page control from Default.aspx page
I have a Label control in a MasterPage.master. How can I to modify this Text propierty from Default.aspx. The master page of default.aspx is MasterPage.master. Thanks Hi, you could expose the label's text as a public property on the master page. In the webform use the @MasterType directive and you can strongly typed call the property. Take a look at this page: http://msdn2.microsoft.com/en-us/library/xxwa0ff0.aspx. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons....

How can I access a control on a master page from a user control using properties?
Hi, I have a master page which I'm using on my aspx page.  Within this aspx page is a user control and I would like to set a label on my master page using a value from my user control. I can do this using FindControl as shown below but I would like to use properties in my master page. The following code in my user control works perfectly: Label theFormName = (Label)this.Page.Master.FindControl("lblFormName");theFormName.Text = "My Test Form"; However I've been told that using properties is a much better ideas (errors would be picked up at compile time an...

Accessing a master page's control in a nested content Page(Urgent)
Hi all, I have a masterpage rm_master.masterand another masterpage rm_sub.master which inherits from rm_master.master. Now I want to access a dropdownlist control in a content page which derieves from rm_select_master. the code i used wasDropDownList DropDownList1 = (DropDownList)Master.FindControl("DropDownList1");string a; a = DropDownList1.Text;   The error i got was "Object reference not set to an instance of an object". Please help.. Thanks in advance.. Bharath You can try Page.FindControl or use the mastertype directivePlease remember to click “...

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Why in the world am I getting this error? The solutions I have seen either don't apply or don't work.  Here is my master page code:<%@ Master Language="C#" AutoEventWireup="false" CodeFile="TechLMSMasters/MainSide.master.cs" Inherits="MainSide" %> <%@ Register TagPrefix="usercontrol" TagName="Search" Src="~/library/uc/Search.ascx" %> <%@ Register TagPrefix="usercontrol" TagName="UserName" Src="~/library/uc/UserNameDisplay.ascx" %><!DOCTYPE html PUBLI...

setting a controls properties within a master page from aspx page without code behind
I have a custom header control that has a public property TabID declared in the codebehind of the control itself. In asp1.1, I dragged the header control to a page and set the tabid like: <uc1:myheader runat="server" TabID="1" /> inside the aspx page it was placed. Now I would like to put the header control inside a master page, but I'm not sure how to set this property without setting it in codebehind for every page i create.  Is this possible? What is the best way to get this value to the header control as it is different for every page?...

Error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.
when I make a directory on my root, and in that directory I put a masterpage and a web form ... I get always an error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.   Does somebody know what the problem is ? Is it possible you can't set a masterpage in a directory ... ?Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) i've founded ... I had to nest my masterpage ...Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) Hi Joeri Pansaerts, I am receiving the same error as the one posted her...

ERROR Content controls have to be top-level controls in a content page or a nested master page that references a master page.
This all used to work until about 5 minutes ago. Did I change something? For some reason I am now getting this error: Content controls have to be top-level controls in a content page or a nested master page that references a master page. What I have:  I have a masterpage inside of a directory. That directory is a first level directory with its own web.config file....The whole thing is protected by the web.config file above it (in the main public directory). That main public directory has a masterpage as well. I am not currently linking the two masterpages together becuase...

Call a public property(master page) from a content page's code behind file
master.page: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load      Caption = "HALLO Titel" End Sub default.aspx.vb: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loadmaster. <--- doesn't work, master.Caption doesn't existEnd Sub Is it possible to do that? Thanks Hello, yes, it is possible. You have to either expose a property from your Master page or use the FindControl Method. Take a look here.VB .Net Coder - Apologize my C# nescienceBest Regards. I ...

Obtain instance of Main.master from within User Control code behind when user control is referenced from a nested master page
I have a user control that's being referenced from within a nested master page.  I need to FROM WITHING THE USER CONTROL itself (not the other way around, not Master to user control) access a control in the master page because I need to set it's ImageURL.The setup is like this:Main.master (holds a Menu control called Menu1)        Admin.master (inherits Main.master)                   myusercontrol.ascx (is referenced in Admin.master) I cannot figure out how to get an instance to Main.master from within my...

Web resources about - How can I access the property a control on a master page from the master page's code behind. - asp.net.navigation-controls

Resources last updated: 12/22/2015 8:07:00 AM