473,890 Members | 1,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.asp x", 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.Maste r.
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 6451
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.dt d">

<script runat="server">

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed 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:contentpla ceholder id="ContentPlac eHolder1" runat="server">
</asp:contentplac eholder>
</div>
</form>
</body>
</html>

Here's the user control, foo.ascx:

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

<script runat="server">
Protected Sub btnFoo_Click(By Val sender As Object, ByVal e As
System.EventArg s)
Dim mstr As MasterPage
Dim lbl As Label
mstr = Page.Master
If mstr.ID = "barmaster" Then
lbl = mstr.FindContro l("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" ContentPlaceHol derID="ContentP laceHolder1"
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******** ************@i4 0g2000cwc.googl egroups.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.asp x", 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.Maste r.
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.Mast er is Bar)
{
Bar barMasterPage = (Bar)this.Page. Master;
barMasterPage.P rocessBarLabel( 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.Mast er is Bar)
{
Bar barMasterPage = (Bar)this.Page. Master;
barMasterPage.P rocessBarLabel( 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
1648
by: chavdar | last post by:
I need to inherit web user control page, how can I do this? Thanks, Chavdar
4
1823
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 pages in a subsection, e.g. everything in /about/ needs to have <body id="about">. In the past what I've done is set a global variable in index.asp, then include the template to render the page: <% pageid = "about"
2
4826
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 page at runtime during the Page_Load event procedure of the hosting ASPX page. Question: Would it be better to load the user control from the hosting page's OnInit event rather than Page_Load? Does it matter or not? I ask because I understand...
1
1274
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 3 levels. When I select a drop down list that is in the 3rd level user control, it causes a post back, but only the postback event on the main page (1st level) fires, nothing else happens in any of the user controls in the 2nd and 3rd level. Any...
0
1421
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 to another page. plz tell me how to add click event ??? Thanx.
6
3214
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
1442
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 control to a web form, if we want to use that user control in code-behind, then we need to declare it to be protected as well, otherwise it will not work properly, why??? I never pay attention to this untill I start to work with user control.
1
4038
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 wrong. I have simplified the problem with the following example: Using asp.net 2.0, I have a Master page template used on default.aspx. >From within the Content tags on that page I placed a Datalist. It is iterating a User control "Inner" with...
5
1235
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 control properties. So I tried the following: Dim myUserControl As MyUserControl =
1
3516
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 javascript code and how to call its function .please help me its urgent and suggest me any idea about this query...... Thanks & Regards Amit
0
9820
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11227
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10812
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9630
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8016
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3276
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.