ASP 2.0: How to access Master Page from Web User Control on Page?

I

ivanpais

Hi,

I have a Web User Control, Lets say "Foo.ascx", that contains a button
"btnFoo".

I have a Master Page "Bar.master", that has a label "lblBar". This
label is exposed by a public property BarLabelText.

I now have a contentpage "FooBar.aspx", where "Bar.master" is the
master page and in the content section has the control "Foo.ascx".

When the button "btnFoo" is clicked, I want to check if the master page
for the Page.Master is "Bar.master" and then set the value of the label
"lblBar".

The user control can see the Page.Master using the this.Page.Master.
However it does not seem to be able to identify the Master page class
and cannot access the master page's BarLabelText property.

Can anyone shed some insight? How can I access the Master Page's public
properties/methods from a web user control on the page?

Thanks in advance.

Ivan.
 
K

Ken Cox - Microsoft MVP

Hi Ivan,

It's a matter of getting references to the master and its included controls.

Here's the master page, bar.master:

<%@ Master Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.ID = "barmaster"
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Master Page</h1>
<p>
<asp:label id="lblBar" runat="server" text="This is set by
bar.master"></asp:label>&nbsp;</p>
<br />
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

Here's the user control, foo.ascx:

<%@ Control Language="VB" ClassName="Foo" %>

<script runat="server">
Protected Sub btnFoo_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim mstr As MasterPage
Dim lbl As Label
mstr = Page.Master
If mstr.ID = "barmaster" Then
lbl = mstr.FindControl("lblBar")
If Not IsNothing(lbl) Then
lbl.Text = "Set by the button click"
End If
End If
End Sub
</script>

<asp:button id="btnFoo" runat="server" onclick="btnFoo_Click" text="I'm
btnFoo" /><br />
<br />
<asp:Label runat="server" Text="I'm inside foo.ascx"
id="lblfooascx"></asp:Label>

Here's the aspx page. FooBar.aspx:

<%@ Page Language="VB" MasterPageFile="~/bar.master" Title="Untitled Page"
%>
<%@ register src="Foo.ascx" tagname="Foo" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<uc1:foo id="Foo1" runat="server" />
</asp:Content>

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
 
I

ivanpais

Hi Ken,

Thank you for the response. It helped me a good deal. The solution that
you provided works well when I try to change any property of a control
directly in the Master page.

In more complex scenarios this did not work well. For example the
operation on the master page was multi-step or accessing a public
method (non-control related) in the Master page.

I found that If I placed a directive in the user control aspx page as
follows:
<%@ Reference Control="~/Bar.master"%>

it would allow me to refenece the master page class.

I created a public function in the master page called ProcessBarLabel
to perform the processing and change the text.
After that I just used the following section of code to invoke the
method:

if (this.Page.Master is Bar)
{
Bar barMasterPage = (Bar)this.Page.Master;
barMasterPage.ProcessBarLabel(message);
}

This worked for me.

Thanks & regards,

Ivan.
 
I

ivanpais

Hi Ken,

Thank you for the response. It helped me a good deal. The solution that
you provided works well when I try to change any property of a control
directly in the Master page.

In more complex scenarios this did not work well. For example the
operation on the master page was multi-step or accessing a public
method (non-control related) in the Master page.

I found that If I placed a directive in the user control aspx page as
follows:
<%@ Reference Control="~/Bar.master"%>

it would allow me to refenece the master page class.

I created a public function in the master page called ProcessBarLabel
to perform the processing and change the text.
After that I just used the following section of code to invoke the
method:

if (this.Page.Master is Bar)
{
Bar barMasterPage = (Bar)this.Page.Master;
barMasterPage.ProcessBarLabel(message);
}

This worked for me.

Thanks & regards,

Ivan.
 
Joined
Apr 12, 2007
Messages
1
Reaction score
0
Web userControl Access MasterPage methods/properities

Hello All,
I created a masterpage and add some methods and espose some properities as contentplaceholder, then I created a user control (.ascx) and placed it on that Masterpage.

Now, I want to add code to the user control that access the masterpage.
How can I acheive that ...

I can access the controls on the masterpage as follows:
ContentPlaceHolder CP = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");
CP.Controls.Add(new LiteralControl("Hellooooooooooooooooooooooz"));

But not the properities nor the methods ...

Thanks and best regards
Waleed Seada
 
Last edited:
Joined
Jun 18, 2010
Messages
3
Reaction score
0
Reference to masterpage and objects there...

If you want access to the masterpage from a page that already inherits from the masterpage, you (obviously) dont need to add the reference in the aspx file.

Say SiteMaster is the class name of my MasterPage

Indide the page that inherits from the masterpage do:

SiteMaster siteMaster = (SiteMaster)this.Page.Master;
myObject= siteMaster.TheObjectIwant;

if you want access to the controls (TheObjectIwant) in the masterpage, add a get/set method with a public modifier in your masterpage.

Cheers,


(\__/)
(='.'=)
(")_(")

Help Bunny by Copying and pasting Bunny into your posts/web page to help him gain world domination.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top