473,289 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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 6359
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chavdar | last post by:
I need to inherit web user control page, how can I do this? Thanks, Chavdar
4
by: Mark | last post by:
I am setting up an asp.net site using the Ektron CMS. Every page in the site uses the same basic template, with the look of the page changing via CSS and an id on the body. The id is shared by all...
2
by: Frankie | last post by:
I have a user control into which I insert a bunch of controls dynamically. I have it all working just fine - Everything is there on Postback, etc. I load this user control into a hosting ASPX...
1
by: Dood | last post by:
I have a page that has a tab control which toggles which user control is loaded into a placeholder. The user controls that are loaded into this place holder have other user controls in them, making...
0
by: conckrish | last post by:
Hi all, Can anyone tell me how to add Click event for dynamically created User control page ?? I have a user control page in datagrid cell. when i click this user control page it ll redirect...
6
by: conckrish | last post by:
Hi all Can anyone tell me how to add a click event for a user control page dynamically??? Thanx Krish.
1
by: zhuang | last post by:
When we drag and drop any server control from toolbox to web form, the relevant control is added to code-behind automatically with access level to be protected. When we drag and drop a user...
1
by: yoknows | last post by:
Hello .Net Gurus. This is my first post here so I apologize in advance if I have not provided the right information. I hope someone has seen this problem before and can tell me what I am doing...
5
by: shapper | last post by:
Hello, I have been trying to add a user control at runtime to a page: Dim myUserControl As Control = LoadControl("MyUserControl.ascx") The problem is this does not give me access to the user...
1
by: amitsaxena1981 | last post by:
hi, I m creating gridview user control with asp.net2.0 and vb.net.Now i want to apply the some javascript function and html tags in gridview user control.I dont know where i put this...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.