|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
February 16th, 2006, 10:59 AM
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Master Page - User Control communication
HI,
I have a master page with a user control on it. How do I change the user control's properties from a content page?
Thanks!
|
February 16th, 2006, 02:41 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Aaron,
You can make the control publicly available through a property. The following snippet exposes a <div> tag called Footer defined in the markup as a public property:
public HtmlGenericControl PageFooter
{
get
{
return Footer;
}
}
You may be tempted to access that property like this in the code behind of a content page:
this.Master.PageFooter.Visible = false;
While this is indeed the code you need, you need something else to make this work. You need to add a MasterType directive to the markup part of the page:
<%@ MasterType VirtualPath="~/MasterPages/MasterPage.master" %>
Instead of VirtualPath, you can also use the TypeName property, but not both.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
February 16th, 2006, 02:52 PM
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for responding Imar,
Where do you declare that property? In the conrol or in the Master page?
|
February 16th, 2006, 03:58 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You declare the public property in the Code Behind for the Master Page. From my example, you can see I am exposing a property of type HtmlGenericControl (a <div> tag in my case).
However, you need to change the property definition to match your User Control type. In addition, you may need to import the namespace that your user control lives in.
The <%@ MasterType line should go in the Content page.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
February 16th, 2006, 06:08 PM
|
Friend of Wrox
|
|
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks.
|
June 15th, 2006, 05:47 PM
|
Registered User
|
|
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
On a related note:
If you ever have a need to access properties of a master page from a user control in ASP.NET 2.0, hereâs how you do it:
Add to .ASCX file:
<%@ Reference VirtualPath="~/Templates/ListDetailTemplate.master" %>
Cast the reference as follows:
((ASP.templates_listdetailtemplate_master)Page.Mas ter).PageHeightOffset = 250;
|
April 12th, 2007, 07:36 AM
|
Registered User
|
|
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by spencery
If you ever have a need to access properties of a master page from a user control in ASP.NET 2.0, here?s how you do it:
Add to .ASCX file:
<%@ Reference VirtualPath="~/Templates/ListDetailTemplate.master" %>
Cast the reference as follows:
((ASP.templates_listdetailtemplate_master)Page.Mas ter).PageHeightOffset = 250;
|
Is this correct for a usercontrol setting on the master page itself?
I mean I have master page and usercontrol (navigation tree) setting on the left side, this control need to access the content place holder on that master page, how can I do that ..
Thanks and best regards
Waleed Seada
|
March 4th, 2008, 10:27 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
And about events? How can i to call an user control´s event in Masterpage?
I hava a button in user control, and this button have an event registered in Load method from a user control.
However, when i clicked in the button, nothing happens.
What i´m doing wrong?
|
March 7th, 2008, 08:52 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The solution has three parts:
1.) Expose the user control as a property in the code behind of your master page
ex:
public ASP.usercontrols_emailform_ascx EmailForm
{
get
{
return ucEmail; //this is the id of the control on the master page
}
}
2.) Register your control both on the master page and on the aspx page
ex:
<%@ Register TagName="EmailForm" TagPrefix="uc" Src="~/UserControls/EmailForm.ascx" %>
3.) Cast Master to your master page class and then access the exposed control
ex:
ASP.usercontrols_emailform_ascx ucEmail = ((ASP.mainmaster_master)Master).EmailForm;
My thanks to previous posters whose posts helped me figure out all of the details
|
|
|