Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

8 views
Skip to first unread message

ivan...@gmail.com

unread,
May 12, 2006, 3:43:24 PM5/12/06
to
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.

Ken Cox - Microsoft MVP

unread,
May 12, 2006, 7:15:45 PM5/12/06
to
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]

<ivan...@gmail.com> wrote in message
news:1147463004....@i40g2000cwc.googlegroups.com...

ivan...@gmail.com

unread,
May 15, 2006, 12:23:30 PM5/15/06
to
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.

ivan...@gmail.com

unread,
May 15, 2006, 12:23:45 PM5/15/06
to
0 new messages