Ehsan Mahpour’s Weblog

How can we pass a value between an ASP.NET 2.0 page and its master page?

by mahpour on Oct.19, 2006, under ASP.NET

The routine for passing data around remains similar for new ASP.NET constructs, such as Master Pages. From the aspect of passing data around, the master page acts as a control within the page. So, as with the earlier page/control techniques, to call a public method located within a page’s master page, you must cast the master reference to the exact class name:

Dim MP As MyMasterPage

MP = CType(Me.Master, MyMasterPage)

MP.MyPublicMasterPageSub()

‘The above 3 lines can alternatively be simplified to one:

CType(Me.Master, MyMasterPage).MyPublicMasterPageSub()

To pass data from the master page to the page, the master page should raise an event to the page:

Public Event MasterPageButtonClicked()

Then the event can be raised to the page with a single line of code:

RaiseEvent MasterPageButtonClicked()

The page can handle the event just as if it were coming from a user control:

Private WithEvents _MyMasterPage As MyMasterPage

Private Sub MyMasterPage_ButtonClicked() _

Handles _MyMasterPage.ButtonClicked

Response.Write(”Master page raised button click event.”)

End Sub

Tip:

To access to some controls on the MasterPage you have to create some kind of interface like a function or subroutine to be able to have access to your controls on that master page.


Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...