Sunday, August 9, 2009

Handle master page click events in content page

Step 1 : Define the property in the Master Page for the LinkButton lnkbtnFromMasterPage

Public LinkButton lnkbFromMasterPage(){

Get {

Return lnkbtnFromMasterPage;

}

}


Step 2 : Reference the Master Page as a casted object in the Content page.This can be done 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 CustomMasterPageCls.

CustomMasterPageCls cmp = ((CustomMasterPageCls)(Master));

Step 3 : Now we can access any Public properties, methods and members of our CustomMasterPageCls 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.

If (!(IsNothing(cmp))) {

cmp.lnkbtnFromMasterPage.Click += New EventHandler(lnkbtnFromMasterPage_Click);

End If


Step 4 : Define the actual method to handle the event.


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

'Perform Business Logic Here

}

No comments: