Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 16th, 2006, 10:59 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!

Reply With Quote
  #2 (permalink)  
Old February 16th, 2006, 02:41 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
  #3 (permalink)  
Old February 16th, 2006, 02:52 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for responding Imar,

Where do you declare that property? In the conrol or in the Master page?

Reply With Quote
  #4 (permalink)  
Old February 16th, 2006, 03:58 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
  #5 (permalink)  
Old February 16th, 2006, 06:08 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.

Reply With Quote
  #6 (permalink)  
Old June 15th, 2006, 05:47 PM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;



Reply With Quote
  #7 (permalink)  
Old April 12th, 2007, 07:36 AM
Registered User
 
Join Date: Apr 2007
Location: Cairo, , Egypt.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #8 (permalink)  
Old March 4th, 2008, 10:27 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?




Reply With Quote
  #9 (permalink)  
Old March 7th, 2008, 08:52 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control not rendering in Master Page skmcusp ASP.NET 2.0 Basics 2 September 10th, 2007 04:58 PM
Access Master page control from Content page. angshujit ASP.NET 2.0 Basics 3 January 11th, 2007 06:20 AM
User object in Master Page template? lancer ASP.NET 2.0 Basics 0 June 22nd, 2006 01:32 PM
how to reference user control in master page.. gbianchi ASP.NET 2.0 Basics 1 May 1st, 2006 09:26 AM



All times are GMT -4. The time now is 06:23 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.