Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access var in MasterPager

Status
Not open for further replies.

mlee1steph

Programmer
May 23, 2004
73
US
Hi Everyone,
I'm working on my first ASP.NET app and have been trying to access a var located in the MasterPage. I've tried using a Property to access the var but could not get that to work, so it was suggested that I try events. So here is the standard flow I'll need:
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 UserInfoEventArgs
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 UserInfoEventHandler

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

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

In the MasterPage:
AddHandler Alarm, AddressOf OnCurrentUser

Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs )
_CurrentUser = e.UserInfo
End Sub
Note: I get a syntax error on the AddHandler line. I read somewhere that ASP.NET handles events differently than Windows apps. So if AddHandler is not correct, then what should I be using.

Do I have this setup correctly for an ASP.NET page? Thanks for any suggestions.
Michael
 
You can access a Public variable or Property of the MasterPage via the Master property of your form.

You will need to cast it to the actual type of the partial class in your masterpage before you can see it.

Code:
If DirectCast(Master, myMasterPageClass).myVariable = 0 Then
.
.
.


Bob Boffin
 
Having read your query a bit more closely you seem to be under a misapprehension. The MasterPage is incorporated into whichever page is curently being constructed that is based on it and cannot be used to persist information between pages as it only ever exists as part of an aspx page. Hence the term 'Partial Class'.

If you wish to persist information you will need to store it in a Session variable, save in in a database or pass it via a QueryString. Of these Session variables are the easiest to use.

It's also worth noting that the order in which the Init and Load events for the page and the masterpage occur may not be what you expect. If I recall correctly the Page_Init occurs before the MasterPage Init so you can switch your masterpage at this point and the same applies to the Page_Load and MasterPage Load events.


Bob Boffin
 
Hi Bob,
Thanks for the reply. I got the event to work correctly now, but based on what you've said then maybe I need to go a different way. I thought that the variable would stay active for the life of the page. So, if I understand you correctly then the var data is not saved in the page between page requests? Humm, I guess I better go back and check out the Session state objects. Thanks for the info.
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top