Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Master Page properties from UC code behind 3

Status
Not open for further replies.

jbenson001

Programmer
Jan 7, 2004
8,172
US
Hello all,

I have a user control that is placed on page that uses a Master Page. From the codebehind of the user control, I would like to set some properties on the master page. I haven't found a way to do it.

I've tried several version of this:
me.Page.Master.title.. however it doesn't recognize title as a property.

Is this even possible, or am I going about it in the wrong way?

I am able to set the title, for example, from the page's code behind using refernced properties from the UC. However I don't think that is the proper way.

Thanks for any help...
Jim
 
you should be able to access the master page from the user controls page object. Casting will probally be required as the user control doesn't know public properties of the specific master page.
Code:
[COLOR=green]
//from within the user control code

//get a reference to the master page[/color]
MasterPage mPage = this.Page.Master;
[COLOR=green]
/*
MyMaster is the name of the master page specific to your project. the code behind of your master page looks like this

public partial class MyMasterPage : MasterPage
{
   public object MyProperity { get... set... }
   ...
}

So you must cast to that specific object to access controls/properties/functions specific to that class.

if your website contains multiple Master Pages you will need a parameter and function to return the correct type.
*/[/color]
MyMasterPage myMaster = (MyMasterPage)mPage;
myMaster.MyProperity = ...;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the response Jason..
I did think of casting the page to a master page of the correct type but I couldn't get that to work. Possibly because there are multiple master pages as you have stated. In that case how could I cast it correctly?

Thanks again..

Jim
 
One way to do this is to set the Title tag on the MasterPage as a server control
Code:
<title runat = "server" id="MasterPageTitle" >Untitled Page</title>

And in the code behind for User Control you can set the HTMLTitle for the Master page like this:
Code:
        ((System.Web.UI.HtmlControls.HtmlTitle) this.Page.Master.FindControl("MasterPageTitle") ).Text = "MasterPageTitle";

Hope this helps.

Sunil
 
The method I was thinking of involves passing the Master Page to the user control by reference. So, in your user control add a function like this:
Code:
    [Blue]Public[/Blue] [Blue]Sub[/Blue] AddPageTitle(ByRef WebPage [Blue]As[/Blue] Page)
        WebPage.Title = [Red]"Test from user control"[/Red]
    [Blue]End[/Blue] [Blue]Sub[/Blue]
Then, on your page load event of the Master Page, you simply have to call the user control's method e.g.
Code:
    [Blue]Protected[/Blue] [Blue]Sub[/Blue] Page_Load([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] [Blue]Me[/Blue].Load
        WebUserControl1.AddPageTitle(Page)
    [Blue]End[/Blue] [Blue]Sub[/Blue]
This method has the advantage of keeping the user control re-usable regardless of which Master Page (or Page) it is on whereas the other methods will fail if they don't have the relevant controls or properties.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
ca8msm, wouldn't your example
ca8msm said:
Then, on your page load event of the Master Page, you simply have to call the user control's method e.g.
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        WebUserControl1.AddPageTitle(Page)
    End Sub
only work if the User Control was placed within the Master Page? shouldn't this code be part of the Web Form and not the Master Page?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
jmeckley said:
wouldn't your example only work if the User Control was placed within the Master Page?
Yes, as that's where I thought Jim's user control was placed (I realise now I'd mis-read the first line of his post). You're correct that if the user control isn't on the Master Page, then you'll have to add the title from the content page instead.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Sunil..
With the help of someone here at my job, we came up with the exact solution your proposed, just before I left for the day. It does work since who ever created the master page created some tags as server controls. Thanks for your post.

Mark..
Thanks for your suggestion.. I will give this a try as well..Thanks for your help

Thank you everyone...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top