Accessing user controls from pages within Master pages


 I have a problem with a web-site I have built which users Master Pages.  One of the pages has a userControl within it, and in the codebehind on the page being loaded into the master page, I access methods and properties on this control.  All builds fine.

When I run it, the control does not load, and looking at the source code, the reason is that the control is prefixed with the name of the container of the master page, "ctl00_contentPage" which stops this control loading properly.  As soon as I amend the HTML generated and delete this prefix from the control name it works fine!

Any ideas?



10/3/2007 9:51:50 AM 13714 articles. 0 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 27

Use Control.ClientId to get the correct Id

Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
10/3/2007 11:12:04 AM

Ok, I can see how using Control.ClientID would get me the server control identifier, but how should this be used in the code behind for my page being loaded into the master page?  To give you more of an idea, I am using a map userControl in my page, and have code like:-

HotelMap.Zoom = 10;

within the page to access properties and methods of that user control.  This control ID gets modified at run-time, so how would I use the ClientID property in this context?

 Thanks for replying...

10/3/2007 11:28:13 AM

Can i look at your complete code, where you are accessing it so that I can guide you. Please include details. ClientId will provide the actual id on the client.

Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
10/3/2007 11:54:51 AM

No Problem.

The masterpage contains the following element into which the other pages are loaded:-

<div id="contentmiddle">

<asp:contentplaceholder id="contentPage" runat="server">



The page location.aspx being loaded into the masterpage contains the following page directives:-

<%@ Page Language="C#" MasterPageFile="~/EjMasterPage.master" AutoEventWireup="true" CodeFile="location.aspx.cs" Inherits="location" Title="Location page" %>
@ MasterType TypeName="EjMasterPage" %>
@ Register Src="LiveMap.ascx" TagName="LiveMap" TagPrefix="uc1" %>

The control is loaded into the page in the following code within the <BODY> of the page

     <uc1:LiveMap ID="HotelMap" runat="server" />

In the codebehind for location.aspx, the following code accesses properties and methods of this control:-

HotelMap.Zoom = 10;

foreach (Photo photo in Photos.PhotoCollection)
if (photo.Latitude != 0)
         HotelMap.AddPin("<img src=" + photo.SmallUrl + " />", photo.Title, Convert.ToDouble(photo.Latitude), Convert.ToDouble(photo.Longitude), "images/camera.jpg");



The HotelMap control creates javascript which is injected into the head of the page in the RegisterMap() method.

The resultant page fails to load this control because the reference to the control above gets changed to:-

<div class="map contain">
	<div class="mapBorder">
		<div id="ctl00_contentPage_HotelMap_Map" style="height:300px;width:300px;position:absolute;">	
Let me know if you need more info and apologies for the length of the post!
10/3/2007 12:16:30 PM

Hold on.....

 The problem is with the control that is generating the javascript loaded into the head tag - changing this to this.ClientID should do the trick.....

Think you've nailed it, will test and mark as answered soon after!



10/3/2007 12:30:26 PM

Include <%=UserControlName.ClientId%> in javascript and thats it. Is it sorted?

Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
10/3/2007 1:07:40 PM

Similar Artilces:

Need to access a Label Control on the Master Page from a user control attached to a content page
Hello all, I have a master page (say  site.master)  with a status Label in it. I have a content page ( say content.aspx)  that has a user control (say usercontrol.ascx). One of the Button Click Events in the usercontrol.ascx needs to pass on some text onto the status label on the masterpage(LabelStatusMsg) and display it. I have been able to do this directly off a content page (content.aspx) by using the following code (VB)  on the master page <%@ MasterType virtualpath="~/site.master" %> and Dim mpLabel As Label = CType(Master.FindControl("LabelStatusMsg"),...

Accessing a control inside of a User Control within a master page using jscript
To be more precise, I'm using a master page called Main.Master, i have one web form called default.aspx.  It contains nothing. I use user controls to display everything.  For simplicity sake the user control here will be called newproject.ascx. This user control has several text boxes and a save button.  When the save button is clicked, I want to utilize the OnClientClick event and use javascript to validate the values in all the controls before passing them to sql. The source for the button is as follows: <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btn...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

Using controls from pages, master pages, and user controls
How would I structure my site in this scenario: I'd like to use a dropdownlist all over my site, sometimes in a regular page, sometimes in a master page, and sometimes in a user control.  I wish to bind its events programmatically, so normally I would just put that in the codebehind.  However, I notice a lot of code duplication because I have to declare the control in everywhere, in my base page (which all my pages inherit from), in my base master page (which all my master pages inherit from) and in my base user control (which all my user controls inherit from).How should I be...

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

How to access a master page control in a user control.
Hi All, I need to access a scriptmanager control placed in a Master page. The scriptmanager is used to RegisterClientScript in the Custom User Control.  The UserControl is placed in a child web page of the Master page. How do I do this? Thanks in advance, Rohan.      There's a ScriptManagerProxy control you can use for this; just drop it onto the user control and you can use that instead....

access controls in user control on a master page
Have a simple question.. I have a dropdown list in a user control within a master page.When I change selection in ddl, I want to access value on the content page.For that, right now I need to refresh the page.. don't know why.. one way around this cud be to declare a public property for UserControl and access it, but I can ONLY see this UserControl in MasterPage... not in a content pageWhile rendering, first content page loads, then MasterPage.. so I can not access changed/current value in the dropdown.. Can someone help?-------------------------------------------------Click on &...

How do I access a control in a page from within a user control?
Hi all, I'm trying to access a hidden field in a page from within a user control but whatever I try just doesn't work. So how can I access controls in a page from within a user control? Thanks use this in ur user control HiddenField HF= (HiddenField)this.Page.FindControl(""HdnControlID");Mark the post as answer if it helps you!Ashish Agrawal----------------------------MCPD(.NET Framework 2.0,Web Developer)MCTS(SQL Server 2005) Make sure the hidden field runs at server (runat="server")Jef Claes~~~~~~~~~~Visit my blog HiddenField HF= (HiddenField)this....

How to access page control (editor control) from user control?
Hi, I want to do something, but I don't know if it's even possible. I'm trying to make a simple application to edit my website. I have a page called editPage.aspx which contains an editor control. (cuteEditor from cutesoft) I also have a Treeview that dynamically populates my website structure. I placed this treeview in a custom control (populateTreeView.ascx) and placed it on the editPage.aspx page. When you click on a html file in the treeview (inside the custom control) I check if the file exists. If it does exists I send it to a class which retrieves the editable region within this ...

Problem Accessing Page Controls in a page that has a master page.
Hi,  I have a problem with accessing the controls of a page that has a masterpage. I am trying to access the Page.Controls from the code behind in the class file but no controls are returned.I can access single controls (ex. txtAddress.Text) but not the whole array of controls (ex. Page.Controls).  The controls structure is..                                 MasterPage  >  PlaceHolder > MyPage.aspx &n...

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

Master Control
I find Master Page idea brilliante. But wouldn't it be nice to have an ability to assign master layout for user controls? Does anyone know of something like that? Look into Themes and Skins. Skins are user to set a template for a contol.Themes and SkinsThemes and Skins 2 Hmm Sean, themes and skins will only help with STYLING the components not layout. What I need is something to set the LAYOUT of the user control and use that master layout to place components in. Like in master page but in user controls.Simple example: Master Control fileUsercontrol layout st...

Finding a control in the master page from within a control on the "child" page....
I've seen several examples of finding controls, but I'm struggling with this.  I want to turn a panel to visible and set a label's text field from within a control... All the examples that I've seen, including the really good one at MSDN2 shows Master.FindControl.  When I use that, it says Master is not declared.  Is there an easy way to set the panel and the label from within the control? Am I close? '---------- My Master page...abbreviated... <%@ Master Language="VB" CodeFile="Secure_Master.master.vb" Inherits="MasterPage" %><%@ Register TagPrefix="cc" TagName="men...

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

Web resources about - Accessing user controls from pages within Master pages -

Highest Percentage Of Opera Mini Users Accessing Facebook? Macau
If you were asked to guess which country had the highest percentage of users of Opera mobile Web browser Opera Mini users accessing Facebook ...

International Users Accessing Facebook Places Through US VPN Accounts
By using a virtual private network (VPN) hosted in the United States, Facebook users from around the world are accessing Facebook Places. The ...

What are some alternatives to Yodlee for accessing bank information?
Clay Loveless , Founder, Jexy. Co-founder, Mashery. Founder, Jexy. Co-founder, Mashery.

Cloud Console - Accessing files in cloud storage for iPad on the iTunes App Store
Get Cloud Console - Accessing files in cloud storage on the App Store. See screenshots and ratings, and read customer reviews.

Meryl Streep On Accessing The Characters Within - YouTube
Meryl Streep talks about the importance of an actors work representing their ability. CONNECT WITH AFI: ...

Accessing the Future Fund a sensible proposal, says super expert Daryl Dixon
As portfolios go, the Future Fund's asset allocation is concentrated in higher-risk investment, including equities and alternative assets.

Accessing my Super
I'm 57 - can I access my super?

Australia, US accessing Indonesian telephone data, leaked documents show
Newly disclosed documents from former US intelligence contractor Edward Snowden have revealed that Australian intelligence efforts against Indonesia ...

Euthanasia advocate Philip Nitschke detained at Melbourne Airport, accuses Customs officials of accessing ...
Euthanasia advocate Philip Nitschke has accused Customs officials of accessing hundreds of patients' sensitive records on his laptop after detaining ...

Frances Abbott scholarship: Sydney woman pleads guilty to accessing Whitehouse Institute records on PM's ...
A Sydney woman who leaked the student records of Tony Abbott's daughter pleads guilty. A Sydney woman who leaked the student records of Prime ...

Resources last updated: 12/6/2015 11:12:31 AM