Reference A var on Master Page from User Control

Discussion in 'ASP .Net' started by =?Utf-8?B?TWljaGFlbA==?=, Mar 31, 2006.

  1. 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
     
    =?Utf-8?B?TWljaGFlbA==?=, Mar 31, 2006
    #1
    1. Advertising

  2. 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
    ' <@pagedelcaration put:
    ' <%@ MasterType VirtualPath="~/MasterPage.master" %> Like this:
    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
    MaintainScrollPositionOnPostback="true" AutoEventWireup="false"
    CodeFile="Auction.aspx.vb" Inherits="Auction" title="Auction" %>

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

    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" <> wrote in message
    news:...
    >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
    >
     
    S. Justin Gengo, Mar 31, 2006
    #2
    1. Advertising

  3. On Fri, 31 Mar 2006 11:30:03 -0600, 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
    >


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

    ex:
    this.Page.MasterPageFile

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

    --
    Craig
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Mar 31, 2006
    #3
  4. 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
    > ' <@pagedelcaration put:
    > ' <%@ MasterType VirtualPath="~/MasterPage.master" %> Like this:
    > <%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
    > MaintainScrollPositionOnPostback="true" AutoEventWireup="false"
    > CodeFile="Auction.aspx.vb" Inherits="Auction" title="Auction" %>
    >
    > <%@ MasterType VirtualPath="~/MasterPage.master" %>
    >
    > 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" <> wrote in message
    > news:...
    > >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
    > >

    >
    >
    >
     
    =?Utf-8?B?TWljaGFlbA==?=, Mar 31, 2006
    #4
  5. Hi Craig,
    Thanks for the reply, I tried using
    Dim ms as MasterPage = CType(Me.Page.MasterPageFile, 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
    > <> 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.MasterPageFile
    >
    > then cast that appropriately to the MasterPage type to access your
    > property.
    >
    > --
    > Craig
    > Microsoft MVP - ASP/ASP.NET
    >
     
    =?Utf-8?B?TWljaGFlbA==?=, Mar 31, 2006
    #5
  6. 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" <> wrote in message
    news:...
    > 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
    >> ' <@pagedelcaration put:
    >> ' <%@ MasterType VirtualPath="~/MasterPage.master" %> Like this:
    >> <%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
    >> MaintainScrollPositionOnPostback="true" AutoEventWireup="false"
    >> CodeFile="Auction.aspx.vb" Inherits="Auction" title="Auction" %>
    >>
    >> <%@ MasterType VirtualPath="~/MasterPage.master" %>
    >>
    >> 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" <> wrote in message
    >> news:...
    >> >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
    >> >

    >>
    >>
    >>
     
    S. Justin Gengo, Mar 31, 2006
    #6
  7. On Fri, 31 Mar 2006 13:58:03 -0600, Michael
    <> wrote:

    > Hi Craig,
    > Thanks for the reply, I tried using
    > Dim ms as MasterPage = CType(Me.Page.MasterPageFile, 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...change MasterPageFile to just Master

    Dim ms as MasterPage = CType(Me.Page.Master, 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
     
    Craig Deelsnyder, Mar 31, 2006
    #7
  8. 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
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Mar 31, 2006
    #8
  9. 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 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.

    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
    > >
     
    =?Utf-8?B?TWljaGFlbA==?=, Apr 6, 2006
    #9
  10. Hi Michael,

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

    AddHandler NameOfObject.Event, AddressOf MethodHandlingTheEvent
    e.g.
    AddHandler LoginInfoObject.Alarm, AddressOf OnCurrentUser

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

    Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) 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 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.
    >
    > 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
    > > >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 6, 2006
    #10
  11. Hi Phillip,
    Thanks for the reply. Ok, I think I'm almost there. What I did was add the
    following to the Page_Load event of the Master Page:
    AddHandler LogIn1.Alarm , AddressOf OnCurrentUser

    For some reason the event is not kicked off. I stepped through the code, and
    after I hit submit (Login) the code in the Login user control works right,
    and calls the RaiseEvent procedure. But for some reason the Master Page event
    is not kicked off. The Page_Load event of the Master Page is run again after
    clicking the Login button so the AddHandler is being called. Does this make
    since. Thanks for any help.
    Michael


    "Phillip Williams" wrote:

    > Hi Michael,
    >
    > You are 99% done. You just missed the correct syntax for AddHandler. It
    > should be like this:
    >
    > AddHandler NameOfObject.Event, AddressOf MethodHandlingTheEvent
    > e.g.
    > AddHandler LoginInfoObject.Alarm, AddressOf OnCurrentUser
    >
    > You could have also used the handle keyword on your method declaration like
    > this:
    >
    > Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) 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 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.
    > >
    > > 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
    > > > >
     
    =?Utf-8?B?TWljaGFlbA==?=, Apr 6, 2006
    #11
  12. Hi Phillip,
    I got the problem solved: I changed the code as follows:
    Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) Handles
    LogIn1.Alarm

    Now the code is working right. I want to thank you again for the help.

    "Phillip Williams" wrote:

    > Hi Michael,
    >
    > You are 99% done. You just missed the correct syntax for AddHandler. It
    > should be like this:
    >
    > AddHandler NameOfObject.Event, AddressOf MethodHandlingTheEvent
    > e.g.
    > AddHandler LoginInfoObject.Alarm, AddressOf OnCurrentUser
    >
    > You could have also used the handle keyword on your method declaration like
    > this:
    >
    > Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) 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 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.
    > >
    > > 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
    > > > >
     
    =?Utf-8?B?TWljaGFlbA==?=, Apr 6, 2006
    #12
  13. My pleasure.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Michael" wrote:

    > Hi Phillip,
    > I got the problem solved: I changed the code as follows:
    > Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) Handles
    > LogIn1.Alarm
    >
    > Now the code is working right. I want to thank you again for the help.
    >
    > "Phillip Williams" wrote:
    >
    > > Hi Michael,
    > >
    > > You are 99% done. You just missed the correct syntax for AddHandler. It
    > > should be like this:
    > >
    > > AddHandler NameOfObject.Event, AddressOf MethodHandlingTheEvent
    > > e.g.
    > > AddHandler LoginInfoObject.Alarm, AddressOf OnCurrentUser
    > >
    > > You could have also used the handle keyword on your method declaration like
    > > this:
    > >
    > > Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) 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 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.
    > > >
    > > > 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
    > > > > >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 6, 2006
    #13
  14. Hi Michael,

    Yes, you should have placed the event wiring during the page.init. (The
    other solution of using the handles statement of the function signature that
    you got working does the same)
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Michael" wrote:

    > Hi Phillip,
    > Thanks for the reply. Ok, I think I'm almost there. What I did was add the
    > following to the Page_Load event of the Master Page:
    > AddHandler LogIn1.Alarm , AddressOf OnCurrentUser
    >
    > For some reason the event is not kicked off. I stepped through the code, and
    > after I hit submit (Login) the code in the Login user control works right,
    > and calls the RaiseEvent procedure. But for some reason the Master Page event
    > is not kicked off. The Page_Load event of the Master Page is run again after
    > clicking the Login button so the AddHandler is being called. Does this make
    > since. Thanks for any help.
    > Michael
    >
    >
    > "Phillip Williams" wrote:
    >
    > > Hi Michael,
    > >
    > > You are 99% done. You just missed the correct syntax for AddHandler. It
    > > should be like this:
    > >
    > > AddHandler NameOfObject.Event, AddressOf MethodHandlingTheEvent
    > > e.g.
    > > AddHandler LoginInfoObject.Alarm, AddressOf OnCurrentUser
    > >
    > > You could have also used the handle keyword on your method declaration like
    > > this:
    > >
    > > Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs ) 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 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.
    > > >
    > > > 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
    > > > > >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 6, 2006
    #14
  15. =?Utf-8?B?TWljaGFlbA==?=

    www.clubpath.com

    Joined:
    May 26, 2008
    Messages:
    2
    Try using

    Namespace.YourMasterPageName master = (Namespace.YourMasterPageName)Page.Master;

    then you should be able to access your public variable from your user control through:

    master.VariableName;

    hth

    Will

    www.clubpath.com | Toronto Night Club Pictures
     
    www.clubpath.com, Jun 5, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    366
    rooster575
    Dec 2, 2003
  2. Doug McCann
    Replies:
    1
    Views:
    1,038
    William Brogden
    Aug 5, 2004
  3. Zeba
    Replies:
    1
    Views:
    499
    Mark Rae
    Feb 22, 2007
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,442
    Nathan Sokalski
    Mar 4, 2010
  5. SerpentKiss2010
    Replies:
    0
    Views:
    2,497
    SerpentKiss2010
    Apr 6, 2011
Loading...

Share This Page