How to get page event name on master page
Answers (2)
Add AnswerIn your master page’s code behind replace the protected keyword on the event handler to public.
public void LinkButton1_Click(object sender, EventArgs e)
{
//Do Stuff Here
}
IN your content page use the Master Type Directive
<%@ MasterType VirtualPath="~/masters/SourcePage.master"" %>
In the code behind for the content page call the Master event handler as follows
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { this.Master.LinkButton1_Click(sender, e); }
Note the code is C#.
- The @ Master directive is another option. When the ASP.NET master page is accessible using the Master property, this method offers a mechanism to construct a strongly typed reference to the master page.
- There is no need to cast because the outcome is as strongly spelled as stated.
- Example:
<%@ MasterType Virtual Path="~/masters/SourcePage.master"" %>
- Although you don’t need to know the type of the masterpage to use this directive, it really results in the same code as @Scott’s solution.
- Then, let’s say, you may begin using your masterpage by
Master.Title = "My Page Title";
- This will also enable you to call upon events from the master. To locate the master control you need, use Master.FindControl.
- For instance, discover control
HtmlAnchor btnMyImageButton = (HtmlAnchor)Master.FindControl("btnMyImageButton");
- However, I advise using the ImageButton’s OnClick property and setting it to a publicly visible void or Sub on the Master page. Then just call that void/Sub as follows:
Master.ImageButtonClick();