Access function in a Master Page from a User control

After banging my head around and google-ing for few hours ,I managed to find a perfect solution that fit my scenario.

My requirement  was to call a method in Master page from user control(.ascx).

Most examples out there seem to be about calling from content page (.aspx) which is pretty straight forward (links below)

Link 1

Link 2

A lot more can be found by simple google .

Below is the code sample which describes how to do same from user control.

In your user control you would have something like below

If Not Page.Master Is Nothing Then
Dim params() As Object = New Object() {"description", dynamicPageMetaValue}
Dim master As Object = Page.Master
Dim masterType As Type = master.GetType()
Dim method As System.Reflection.MethodInfo = masterType.GetMethod("setMeta")
method.Invoke(Page.Master, params)
End If

  • params() is where you would declare the parameters/values  to pass to your method ,if any.      
  • setMeta is your method name

For more please see the link.

Happy coding Smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s