Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
Handling Master Page Click Events in the Content Page

I had to try something new the other day to solve a problem, setting the Click Event Handler of a button on a Master Page in the Content Page. Honestly I have never had the need, or at least not realized that I needed this functionality up to this point so I was a little hesitant. Needless to say I figured it out and pretty quickly, at least a workable solution.
So the problem I had to solve was how to handle a LinkButon Click event from within the content page for a LinkButton on the Master Page. The LinkButtons need to be displayed on every page the same way, but each page needed to process some unique business logic on each content page, so I really needed to handle the click event in the content page.
I often create public properties to provide access to variables on Master Pages. I also do this with Cross-Page Postbacks to provide access to variables and controls. So I thought this would work.

This is the code that defines the property in the Master Page for the LinkButton lnkbFromMasterPage.

VB

    Public ReadOnly Property lnkbFromMasterPage() As LinkButton

        Get

            Return lnbFromMasterPage

        End Get

    End Property

C#

    Public LinkButton lnkbFromMasterPage(){

        Get {

            Return lnbFromMasterPage;

        }

    }

The next step is to reference the Master Page as a casted object in the Content page. This can be done easy enough by accessing the MasterPage object of the content page and casting it to the class for our Master Page. Here the class defined for the Master Page is CustomMasterPage.

VB 

    Dim cmp As CustomMasterPage = CType(Master, CustomMasterPage)

C# 

    CustomMasterPage cmp = ((CustomMasterPage)(Master));

Now we can access any Public properties, methods and members of our CustomMasterPage class. So here was the magic, would I be able to handle the button events in the Content Page? Of course I can!
You simply need to define the event handler in your Content Page, I do it in the PageLoad event handler.

VB

    If Not IsNothing(cmp) Then

 

        AddHandler cmp.lnkbFromMasterPage.Click, AddressOf lnkbFromMasterPage_Click

 

    End If 

C# 

    If (!(IsNothing(cmp))) {

 

        cmp.lnkbFromMasterPage.Click += New EventHandler(lnkbFromMasterPage_Click);

 

    End If

Later in my code I define the actual method to handle the event.

VB

    Protected Sub lnkbFromMasterPage_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        'Perform Business Logic Here

    End Sub 

C#  

    Protected void lnkbFromMasterPage_Click(Object sender, System.EventArgs e){

        'Perform Business Logic Here

    }

That is all there is to it. The same architecture works for User Controls as well, since that is really what a Master Page actually is. I was satisfactorily surprised at how easy this was, not to mention my first thought on how to solve this problem.

kick it on DotNetKicks.com
Posted: Thursday, January 11, 2007 8:42 AM

by Chris Love
Filed under:

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 5, 2007 9:41 PM

Paul said:

Great! but you could fix the terrible C# translation! ;-)
# March 19, 2008 4:42 AM

Alex said:

Hi, Thanks for the article. I'm having a problem with casting the masterpage. I created a MasterPage.master file in VS2008 and the class in MasterPage.master.cs starts off with: public partial class MasterPage : System.Web.UI.MasterPage so when i cast, do i replace your "CustomMasterPage" with "MasterPage"? I tried that but its not working. Do you know what i'm doing wrong?
# April 1, 2008 1:10 PM

Arun said:

Nice article! Had the exact same situation, but with handling events from UserControls in the content page. Solution worked great! Thanks!
# August 8, 2008 3:26 PM

Praveen said:

i am verymuch thankful to you. its really great. my work is now simplified with your code. please mail me your articles to praven.akki@gmail.com thanks Praveen
# December 9, 2008 6:06 AM

ugh said:

If (!(IsNothing(esmp))) What is esmp? Oh you were so close to showing us something...but we don't know what esmp is.
# February 24, 2009 10:28 AM

yeh right said:

does not work. thanks for the 'education'
# February 24, 2009 12:23 PM

Chris Love said:

ugh, sorry, I copied the example from a real application and seem to have forgot to replace the variable name with the one in the sample code. So it should be cmp.

ugh and yeh right, sorry you two may not see what is happening. I assure you this technique works. I have implemented it on at least 30 sites and hundreds of pages.

# February 25, 2009 11:18 AM

NetRevolutions said:

Chris Love, I need you sample code please!!!! I can't use this line code in my page client CustomMasterPage cmp = ((CustomMasterPage)(Master)); What Happened??!! Thanks!!!!
# May 26, 2009 12:04 AM

Nismoto said:

You can avoid casting the master page from one type to another, as well as checking for nothing/null, by adding the following to your content page: <%@ MasterType VirtualPath="yourmasterpage.Master" %> Then you can access your master page from your content page as follows: this.Master.lnkbFromMasterPage.Click += New EventHandler(lnkbFromMasterPage_Click);
# June 18, 2009 3:25 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS