469,909 Members | 1,927 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

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

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.

May 12 '06 #1
3 6098
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]

<iv******@gmail.com> wrote in message
news:11********************@i40g2000cwc.googlegrou ps.com...
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.

May 12 '06 #2
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.

May 15 '06 #3
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.

May 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by chavdar | last post: by
4 posts views Thread by Mark | last post: by
1 post views Thread by zhuang | last post: by
1 post views Thread by yoknows | last post: by
5 posts views Thread by shapper | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.