473,749 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference A var on Master Page from User Control

I have setup a public variable in the Master Page "code-behind-file". Now I
would like to set that value from the UserControl, but I can't seem to find a
way to do this. Does anyone have any ideas? I'm basically trying to set it up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael

Mar 31 '06 #1
13 6229
Michael,

Do it with a private variable and public property.

Private _UserId As Int32

Public Property UserId() As Int32
Get
Return _UserId
End Get
Set (ByVal value As Int32)
_UserId = Value
End Set
End Property

'---And now here's the trick:
' On any page you want to access the property
' from in the HTML View just below the
' <@pagedelcarati on put:
' <%@ MasterType VirtualPath="~/MasterPage.mast er" %> Like this:
<%@ Page Language="VB" MasterPageFile= "~/MasterPage.mast er"
MaintainScrollP ositionOnPostba ck="true" AutoEventWireup ="false"
CodeFile="Aucti on.aspx.vb" Inherits="Aucti on" title="Auction" %>

<%@ MasterType VirtualPath="~/MasterPage.mast er" %>

The MasterType declaration pointing to the master file in question is what
let's you access properties and other public declarations in the master
page.

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Michael" <Mi*****@discus sions.microsoft .com> wrote in message
news:71******** *************** ***********@mic rosoft.com...
I have setup a public variable in the Master Page "code-behind-file". Now I
would like to set that value from the UserControl, but I can't seem to
find a
way to do this. Does anyone have any ideas? I'm basically trying to set it
up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael

Mar 31 '06 #2
On Fri, 31 Mar 2006 11:30:03 -0600, Michael
<Mi*****@discus sions.microsoft .com> wrote:
I have setup a public variable in the Master Page "code-behind-file".
Now I
would like to set that value from the UserControl, but I can't seem to
find a
way to do this. Does anyone have any ideas? I'm basically trying to set
it up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael


The UserControl has a .Page property, from which you can get its MasterPage

ex:
this.Page.Maste rPageFile

then cast that appropriately to the MasterPage type to access your
property.

--
Craig
Microsoft MVP - ASP/ASP.NET
Mar 31 '06 #3
Hi,
Thanks for the reply. I tried setting up a Public Property, just as you
suggested and I still can't get access to the Property in the Master Page
from the UserControl on that Master Page.
Page.Master.??? ? don't work
I've also tried wrapping Master Page, Usercontrol in the same namespace and
I got other errors (saw this suggestion in another message). Am I trying to
access the master in the wrong way? Or am I missing something. Thanks.
Michael
"S. Justin Gengo" wrote:
Michael,

Do it with a private variable and public property.

Private _UserId As Int32

Public Property UserId() As Int32
Get
Return _UserId
End Get
Set (ByVal value As Int32)
_UserId = Value
End Set
End Property

'---And now here's the trick:
' On any page you want to access the property
' from in the HTML View just below the
' <@pagedelcarati on put:
' <%@ MasterType VirtualPath="~/MasterPage.mast er" %> Like this:
<%@ Page Language="VB" MasterPageFile= "~/MasterPage.mast er"
MaintainScrollP ositionOnPostba ck="true" AutoEventWireup ="false"
CodeFile="Aucti on.aspx.vb" Inherits="Aucti on" title="Auction" %>

<%@ MasterType VirtualPath="~/MasterPage.mast er" %>

The MasterType declaration pointing to the master file in question is what
let's you access properties and other public declarations in the master
page.

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Michael" <Mi*****@discus sions.microsoft .com> wrote in message
news:71******** *************** ***********@mic rosoft.com...
I have setup a public variable in the Master Page "code-behind-file". Now I
would like to set that value from the UserControl, but I can't seem to
find a
way to do this. Does anyone have any ideas? I'm basically trying to set it
up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael


Mar 31 '06 #4
Hi Craig,
Thanks for the reply, I tried using
Dim ms as MasterPage = CType(Me.Page.M asterPageFile, MasterPage )
But I get a error trying to convert string to MasterPage. How do I do this
with the MasterPageFile property? Can you please give me a short example.
Thanks so much for the help.
Michael
P.S. Have you seen any article or books that discuss issues like this with
MasterPages?
"Craig Deelsnyder" wrote:
On Fri, 31 Mar 2006 11:30:03 -0600, Michael
<Mi*****@discus sions.microsoft .com> wrote:
I have setup a public variable in the Master Page "code-behind-file".
Now I
would like to set that value from the UserControl, but I can't seem to
find a
way to do this. Does anyone have any ideas? I'm basically trying to set
it up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael


The UserControl has a .Page property, from which you can get its MasterPage

ex:
this.Page.Maste rPageFile

then cast that appropriately to the MasterPage type to access your
property.

--
Craig
Microsoft MVP - ASP/ASP.NET

Mar 31 '06 #5
Michael,

I'm not positive how to do it from the usercontrol itself, but this may lead
you in the right direction. From the page that the usercontrol is on you
just use Master.[Property]

That's what the MasterType reference lets you do.

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Michael" <Mi*****@discus sions.microsoft .com> wrote in message
news:7A******** *************** ***********@mic rosoft.com...
Hi,
Thanks for the reply. I tried setting up a Public Property, just as you
suggested and I still can't get access to the Property in the Master Page
from the UserControl on that Master Page.
Page.Master.??? ? don't work
I've also tried wrapping Master Page, Usercontrol in the same namespace
and
I got other errors (saw this suggestion in another message). Am I trying
to
access the master in the wrong way? Or am I missing something. Thanks.
Michael
"S. Justin Gengo" wrote:
Michael,

Do it with a private variable and public property.

Private _UserId As Int32

Public Property UserId() As Int32
Get
Return _UserId
End Get
Set (ByVal value As Int32)
_UserId = Value
End Set
End Property

'---And now here's the trick:
' On any page you want to access the property
' from in the HTML View just below the
' <@pagedelcarati on put:
' <%@ MasterType VirtualPath="~/MasterPage.mast er" %> Like this:
<%@ Page Language="VB" MasterPageFile= "~/MasterPage.mast er"
MaintainScrollP ositionOnPostba ck="true" AutoEventWireup ="false"
CodeFile="Aucti on.aspx.vb" Inherits="Aucti on" title="Auction" %>

<%@ MasterType VirtualPath="~/MasterPage.mast er" %>

The MasterType declaration pointing to the master file in question is
what
let's you access properties and other public declarations in the master
page.

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Michael" <Mi*****@discus sions.microsoft .com> wrote in message
news:71******** *************** ***********@mic rosoft.com...
>I have setup a public variable in the Master Page "code-behind-file".
>Now I
> would like to set that value from the UserControl, but I can't seem to
> find a
> way to do this. Does anyone have any ideas? I'm basically trying to set
> it
> up
> so that I can keep the User infor (userid, ect) in the Masterpage so
> that
> other pages can access it. Thanks for any ideas.
> Michael
>


Mar 31 '06 #6
On Fri, 31 Mar 2006 13:58:03 -0600, Michael
<Mi*****@discus sions.microsoft .com> wrote:
Hi Craig,
Thanks for the reply, I tried using
Dim ms as MasterPage = CType(Me.Page.M asterPageFile, MasterPage )
But I get a error trying to convert string to MasterPage. How do I do
this
with the MasterPageFile property? Can you please give me a short example.
Thanks so much for the help.
Michael
P.S. Have you seen any article or books that discuss issues like this
with
MasterPages?


whoops! sleep-deprived friday for me....sorry, that property gives the
filename...chan ge MasterPageFile to just Master

Dim ms as MasterPage = CType(Me.Page.M aster, MasterPage )

sorry, I don't have many links on it, I've used master pages since 1.x so
I don't know of good 2.0 articles; I just did a refresher of the changes
they made in 2.0 by reading it in a book. Here's one article that looks
decent:

http://www.codeproject.com/aspnet/InsideMasterPages.asp

--
Craig
Microsoft MVP - ASP/ASP.NET
Mar 31 '06 #7
Michael,

If I understood your scenario correctly, then you have a master page (which
is a control on the page) that has a child user control that needs to
communicate data (users information that is generated perhaps after some user
input) to the parent control (the master page).

In such scenarios (of communicating data between 2 controls on a web form)
you need to look at using events. When the child user control finishes
collecting the user info, then you raise an event, that would be consumed by
the parent control (the master page), which in turn would display the data on
its control (or do whatever other processing required).

To understand how to raise an event from the child control and how to
consume it by the master page, review these articles on the MSDN:
http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Michael" wrote:
I have setup a public variable in the Master Page "code-behind-file". Now I
would like to set that value from the UserControl, but I can't seem to find a
way to do this. Does anyone have any ideas? I'm basically trying to set it up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael

Mar 31 '06 #8
Hi Phillip,
Thanks so much for the reply. I've started to look into this and have come
across one issue. It seems that AddHandler is not supported in ASP.NET. Is
this true, if so, what includes do I need to include into the project. Here
is what I have so far.
Just a refresher of what I needed:
1. User enter UserId & password and selects submit.
2. The login User control will check what roles they have and create a User
Object.
3. Set the User Object to a Value stored in the MasterPage.
4. Purchasing screen shown and depending on the Role the user has will
determine what the user sees (Admin/Standard user).
So what I have come up with so far based on the event based noticifaction.
Stand alone Class File:
Public Class UserInfoEventAr gs
Inherits EventArgs
Private _User as User

Public sub new(UserInfo as User )
_User = UserInfo
End Sub

Public ReadOnly Property UserInfo() as User
Get
UserInfo = _User
End Get
End Property
End Class

In the Login User Control:
Public Event Alarm as UserInfoEventHa ndler

Protected OverRidable Sub OnAlarm(e as UserInfoEventAr gs )
RaiseEvent Alarm(me, e)
End Sub

Sub AuthenticateUse r(userId as string, password as password)
'Do stuff here.......
'Now feed the UserInfo to the MasterPage via a Event
Dim e as New UserInfoEventAr gs(lUser)
OnAlarm(e)
'Do more stuff....
end sub

In the MasterPage:
AddHandler Alarm, AddressOf OnCurrentUser

Public sub OnCurrentUser(s ender as Object, e as UserInfoEventAr gs )
_CurrentUser = e.UserInfo
End Sub
Note: I get a syntax error on the AddHandler line.

Do I have this setup correctly for an ASP.NET page? Thanks again for any
suggestions.
Michael


"Phillip Williams" wrote:
Michael,

If I understood your scenario correctly, then you have a master page (which
is a control on the page) that has a child user control that needs to
communicate data (users information that is generated perhaps after some user
input) to the parent control (the master page).

In such scenarios (of communicating data between 2 controls on a web form)
you need to look at using events. When the child user control finishes
collecting the user info, then you raise an event, that would be consumed by
the parent control (the master page), which in turn would display the data on
its control (or do whatever other processing required).

To understand how to raise an event from the child control and how to
consume it by the master page, review these articles on the MSDN:
http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Michael" wrote:
I have setup a public variable in the Master Page "code-behind-file". Now I
would like to set that value from the UserControl, but I can't seem to find a
way to do this. Does anyone have any ideas? I'm basically trying to set it up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael

Apr 6 '06 #9
Hi Michael,

You are 99% done. You just missed the correct syntax for AddHandler. It
should be like this:

AddHandler NameOfObject.Ev ent, AddressOf MethodHandlingT heEvent
e.g.
AddHandler LoginInfoObject .Alarm, AddressOf OnCurrentUser

You could have also used the handle keyword on your method declaration like
this:

Public sub OnCurrentUser(s ender as Object, e as UserInfoEventAr gs ) Handles
LoginInfoObject .Alarm
_CurrentUser = e.UserInfo
End Sub

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Michael" wrote:
Hi Phillip,
Thanks so much for the reply. I've started to look into this and have come
across one issue. It seems that AddHandler is not supported in ASP.NET. Is
this true, if so, what includes do I need to include into the project. Here
is what I have so far.
Just a refresher of what I needed:
1. User enter UserId & password and selects submit.
2. The login User control will check what roles they have and create a User
Object.
3. Set the User Object to a Value stored in the MasterPage.
4. Purchasing screen shown and depending on the Role the user has will
determine what the user sees (Admin/Standard user).
So what I have come up with so far based on the event based noticifaction.
Stand alone Class File:
Public Class UserInfoEventAr gs
Inherits EventArgs
Private _User as User

Public sub new(UserInfo as User )
_User = UserInfo
End Sub

Public ReadOnly Property UserInfo() as User
Get
UserInfo = _User
End Get
End Property
End Class

In the Login User Control:
Public Event Alarm as UserInfoEventHa ndler

Protected OverRidable Sub OnAlarm(e as UserInfoEventAr gs )
RaiseEvent Alarm(me, e)
End Sub

Sub AuthenticateUse r(userId as string, password as password)
'Do stuff here.......
'Now feed the UserInfo to the MasterPage via a Event
Dim e as New UserInfoEventAr gs(lUser)
OnAlarm(e)
'Do more stuff....
end sub

In the MasterPage:
AddHandler Alarm, AddressOf OnCurrentUser

Public sub OnCurrentUser(s ender as Object, e as UserInfoEventAr gs )
_CurrentUser = e.UserInfo
End Sub
Note: I get a syntax error on the AddHandler line.

Do I have this setup correctly for an ASP.NET page? Thanks again for any
suggestions.
Michael


"Phillip Williams" wrote:
Michael,

If I understood your scenario correctly, then you have a master page (which
is a control on the page) that has a child user control that needs to
communicate data (users information that is generated perhaps after some user
input) to the parent control (the master page).

In such scenarios (of communicating data between 2 controls on a web form)
you need to look at using events. When the child user control finishes
collecting the user info, then you raise an event, that would be consumed by
the parent control (the master page), which in turn would display the data on
its control (or do whatever other processing required).

To understand how to raise an event from the child control and how to
consume it by the master page, review these articles on the MSDN:
http://msdn2.microsoft.com/en-us/library/17sde2xt.aspx

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Michael" wrote:
I have setup a public variable in the Master Page "code-behind-file". Now I
would like to set that value from the UserControl, but I can't seem to find a
way to do this. Does anyone have any ideas? I'm basically trying to set it up
so that I can keep the User infor (userid, ect) in the Masterpage so that
other pages can access it. Thanks for any ideas.
Michael

Apr 6 '06 #10

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

Similar topics

6
11299
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
10
2506
by: George G. | last post by:
Hi there, I am busy writing a new asp.net application and I am reusing some of my existing asp functions and methods in a user control. I need access to session, request and response in some of the functions and I can't find out how to do it. Here is an example of what I do and I get the following Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the...
3
965
by: adh | last post by:
Hello, How do I get the referebce to the last added UserControl: Me.Controls.Add(myUserControl) When I add one after another? Return CType(Me.Controls(.Controls.Count - 1), _ myUserControl) doesn't go. Thanks, adh
3
1727
by: PJ | last post by:
I am trying to get reference to a Master page from a web user control. However, I cannot seem to even access the type. I have been referencing the master page in forms by declaring the master attribute atop the .aspx page. This works fine, but this attribute is not recognized in controls. Thanks ~PJ
1
1843
by: Charlie | last post by:
Hi: I'm trying to pass a reference to a user control to a class. The user control exposes its elements via properties. When I inspect parameter (user control is passed to class using "this") I can see it's data in debugger, but no way to access it in class code. I have tried casting to control type, but it says type or namespace can't be found. Any ideas? Charlie
1
4029
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...
2
2590
by: o0JoeCool0o | last post by:
I am trying to create a User Control, that will be a message box with input options if I call okconf.visible = true in the page load of the user control it works fine, but if i then try to call it later by calling the user controls alert method i get a reference null error. heres the class code >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1
7741
by: Paul | last post by:
Hello: I want to nlude this statement into everyone of my application's .aspx pages. <%@ Import Namespace="System.Data.SQLClient" %> What is the best practice to do this? Can I do this using a Master Page? User control? Something else?
3
2344
by: Managed Code | last post by:
I have a BasePage class that derives from System.Web.UI.Page. All of my content pages derive from this. The derived page classes use the following MasterType declaration that follows to reference the master. <%@ MasterType TypeName="MyMasterPage" %> How do I reference the master page class from the BasePage? Thanks...
3
2362
by: William Youngman | last post by:
I am on a team that is developing a proposal generation web application and we are using a custom base page (ProGenBase.cs) located in the app_code directory and all of the app's web pages inherit from this custom page. Additionally I have a main menu tab control that is located in a master page and certain tabs are displayed dependant on the users authentication level. I am using boolean properties in the master page to control the visible...
0
8832
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
9566
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
9388
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...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
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...
0
6078
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
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
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
2791
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.