473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I access MasterPage functionality from an App_Code class?

I am trying to access a label located in a MasterPage from a class
located in my application's App_Code folder. The problem seems to be
that the class cannot communicate with the MasterPage due to a lack of
reference to it. How do I supply a reference to the class so that it
can populate the label? My code is below...

*The reason I am doing it this way, is that I want the typical code
used to supply a string to the MasterPage label to be hidden away in
case I change it later on. Using an object to send a string to the
class allows me to do this without committing each and every page to a
specific method.*

``````````````` ``````````````` ``````````````` ``````````````` `
1) So, in my MasterPage I set up a simple Label control:

<asp:label id="lblLabel" runat="server" />

2) In a class stored in App_Code I set up a procedure to populate the
label in the MasterPage:

Public Namespace CustomClasses
Public Class MessageMaker
Public Sub CreateMessage(B yVal strMessage As String)

Dim lblError As Label
lblError = Master.FindCont rol("lblLabel")
lblError.Text = strMessage

End Sub
End Class
End Namespace

In a content page, I want a procedure to create that creates a message
in the MasterPage label. This will be used for all system messages sent
to the user.

Dim objMessage As New CustomClasses.M essageMaker()
objMessage.Crea teMessage("This is a Test")

Feb 22 '06 #1
2 2838
You may find it easier by changing the pattern:

Public Function CreateMessage(B yVal strMessage As String) As String
strMessage="Her e it is" & strMessage
return strMessage
End Function

In your page,
Dim objMessage As New CustomClasses.M essageMaker()
Dim lbl As Label = Master.FindCont rol("label1")
Dim msg as String =Master.FindCon trol(objMessage .CreateMessage( "This is a
Test")
lbl.Text=msg

Otherwise, you would need to figure out a way to pass the page context to
your class. You *CAN* do this if absolutely necessary:

Public Function CreateMessage(B yVal strMessage As String, ByRef Page as
System.Web.UI.P age) As String
' etc.
End Function

Hope that helps.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"dawg1998" wrote:
I am trying to access a label located in a MasterPage from a class
located in my application's App_Code folder. The problem seems to be
that the class cannot communicate with the MasterPage due to a lack of
reference to it. How do I supply a reference to the class so that it
can populate the label? My code is below...

*The reason I am doing it this way, is that I want the typical code
used to supply a string to the MasterPage label to be hidden away in
case I change it later on. Using an object to send a string to the
class allows me to do this without committing each and every page to a
specific method.*

``````````````` ``````````````` ``````````````` ``````````````` `
1) So, in my MasterPage I set up a simple Label control:

<asp:label id="lblLabel" runat="server" />

2) In a class stored in App_Code I set up a procedure to populate the
label in the MasterPage:

Public Namespace CustomClasses
Public Class MessageMaker
Public Sub CreateMessage(B yVal strMessage As String)

Dim lblError As Label
lblError = Master.FindCont rol("lblLabel")
lblError.Text = strMessage

End Sub
End Class
End Namespace

In a content page, I want a procedure to create that creates a message
in the MasterPage label. This will be used for all system messages sent
to the user.

Dim objMessage As New CustomClasses.M essageMaker()
objMessage.Crea teMessage("This is a Test")

Feb 22 '06 #2
Peter:

Thanks for the reply.

My goal, though, is to eliminate the "Master.FindCon trol('label1')" bit
of code from my content page and move that to the class in App_Code.

So, yes, I guess my goal is to pass the page context to the class. Is
the method you detailed a costly one in terms of resource usage?

rb

Feb 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3040
by: Ronnie | last post by:
I've created a simple web application using VS2005 Beta 2. Basically, I've added a Web Form and a Global.asax file. In my Global.asax, I create a method like: public static void TestGlobal() { throw new Exception("Foobar"); }
9
4361
by: Leffe Andersson | last post by:
Hi folks! I have a MasterPage with some public methods, and I would like to call them from a (app_code) class. Is it possible to set a directive or an assembly reference to a MasterPage from a normal class? I have no problem to call the masterpage's methods from a contentpage, but what I would like to do is someting like this... public partial class MyMaster : System.Web.UI.MasterPage
5
7286
by: RedHair | last post by:
I have a ASP.NET 2.0 web page which inherits a customized base page class and have a master page, their relation is as below Customized base page class --> web page --> master page How to access the property and control of master page from the customized base page class? Btw, I can access the server control of master page from base page class via
3
2092
by: Alex Maghen | last post by:
Hi. I'm a little confused about the code that resides in the code-behind of a MasterPage and the code that resides in the code-behind of the actual pages that USE that MasterPage. I'm noticing, for example, that the Page_Load on the specific page executes before the Page_Load of its MasterPage. Is this right? But what I really want to understand is VARIABLE SCOPE, etc. between the two. For example: Is there a way for me to write code...
10
2250
by: Christophe Peillet | last post by:
I am trying to create a BasePage for use in a large asp.net application, that will centrally provide certain extra properties and logic to the application. (The web project makes use Master Pages as well.) I want to make a property named TitleResourceKey, for example, that contains the resource key used to translate the page title and, using reflection and the CustomLocalisationKey attribute that points to the property this key...
2
1656
by: Alex Maghen | last post by:
I want to create a utility function that will seach the current page for one of my UserControls by it's type. So, let's say that I have a UserControl whose class I defined as follows: namespace MyUtils { public partial class SomeUserControl : System.Web.UI.UserControl { ... }
4
3998
by: Boris Yeltsin | last post by:
OK, on my Master Page I have a control: <a id="hypTabAccount" href="#" runat="server">Account</a> Now, in the code-behind (Root.master.vb) I can refer to it simply thus: hypTabAccount.InnerText = "blah" Now, what I want is the same in a content page that uses the Master Page. I have a Master Type in my Content page:
2
2210
by: =?Utf-8?B?RGFsZQ==?= | last post by:
I have an app with a MasterPage. In the MasterPage is a user control with several public properties and methods that I want exposed to the content page. Rather than writing several redirection methods to expose the properties, I would rather just expose the user control as a property of the MasterPage. The problem is, when I compile, I get an exception on the content page: The type 'MessageBlock.MessageBlock' is defined in an...
6
9173
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
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
10376
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
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
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
7660
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
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.