locked
access masterpage property from usercontrol nested inside page using masterpage RRS feed

  • Question

  • User1738843376 posted

    Hi,

    I have created a property on my masterpage

    Public Property FillersInPage As New List(Of Filler)

    This list needs to be updated from within a usercontrol that will be instanced multiple times inside a page that uses the masterpage mentioned above.

    So, it will look something like this:

    • MasterPage ("~/main.master")
      • Page ("~/details.aspx")
        • UserControl1 ("~/app_ctrls/userControl.ascx")
        • UserControl2 ("~/app_ctrls/userControl.ascx")
        • UserControl3 ("~/app_ctrls/userControl.ascx")

    I've tried referencing the masterpage inside the userControl.ascx, so that i could later on use DirectCast to instance and access the FillersInPage property of the masterpage in order to update its contents, but i can't seem to be able to do so.

    I'm using a Website project with vb.net.

    Anyone got a clue on the correct way to do this, if there is one?

    Tuesday, April 28, 2020 12:06 PM

Answers

  • User475983607 posted

    The standard mechanism in Web Forms are events (delegates).  The user control fires an event.  The content page handles the event and throws another event.  The master page handles the content page event.  Essentially this creates a pipeline from the user control to the content page then up to the master page.  

    You can also have the content page update the master page directly using a page directive.  You still need a user control event handler in the content page.  I prefer to use events as because events do not tightly couple the 3 classes. Although you need to wire up the event which is pretty simple anyway. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 12:24 PM

All replies

  • User475983607 posted

    The standard mechanism in Web Forms are events (delegates).  The user control fires an event.  The content page handles the event and throws another event.  The master page handles the content page event.  Essentially this creates a pipeline from the user control to the content page then up to the master page.  

    You can also have the content page update the master page directly using a page directive.  You still need a user control event handler in the content page.  I prefer to use events as because events do not tightly couple the 3 classes. Although you need to wire up the event which is pretty simple anyway. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 12:24 PM
  • User1738843376 posted

    Thanks mgebhard,

    I ended up taking a different approach at my problem, controlling all from the page, instead of using the events as you suggested, but i'll keep them in mind the next time i approach a problem like this :)

    Tuesday, April 28, 2020 6:23 PM