Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| 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 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
  #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!

  #2 (permalink)  
Old February 16th, 2006, 02:41 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
  #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?

  #4 (permalink)  
Old February 16th, 2006, 03:58 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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.
  #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.

  #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;



  #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
  #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?




  #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


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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.