ASP.Net: How to call the main page event handler from the content page event handler?

I have a main page where I have a link button. On one of the content pages, I need to hide the link and replace the image with a button. The click event handler for the image button should do exactly the same thing as the click button event of the link button on the main page. So, is there a way to call the link to the "Link to the main page" button using the event handler for clicking the "Image" button on the "Content" page?

I researched and it seems a lot if the main page wants to call an event handler on the content page ...

Any inout would be highly appreciated.

thanks

+4
source share
3 answers

In the main page code, behind the replacement of the protected keyword in the event handler, publicly.

public void LinkButton1_Click(object sender, EventArgs e) { //Do Stuff Here } 

Use the directive of the main type on your content page

 <%@ MasterType VirtualPath="~/masters/SourcePage.master"" %> 

In the code 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); } 

Please note that the code is C #.

I look more at calling the homepage event handler

+6
source

There is also the @Master directive ( MSDN Article ). This method provides a way to create a strongly typed link to the ASP.NET master page when accessing the master page from the Master property.

The result is the same as indicated, strongly typed, and there is no need to drop.

Usage example:

 <%@ MasterType VirtualPath="~/masters/SourcePage.master"" %> 

Using this directive actually gives the same code as the @Scott implementation, but does not require you to know the type of the main page.

Then you can start using your main page by saying:

 Master.Title = "My Page Title"; 

You can also trigger events from the master in this way. Use Master.FindControl to find the desired management wizard.

Search example:

 HtmlAnchor btnMyImageButton = (HtmlAnchor)Master.FindControl("btnMyImageButton"); 

BUT, I would suggest using the OnClick property for ImageButton and setting it to the public void / Sub on the main page. Then just call it void / Sub like:

 Master.ImageButtonClick(); 
+4
source

If your main page is MyMaster, create a property like this:

 Public Shadows ReadOnly Property Master() As MyMaster Get Return CType(MyBase.Master, MyMaster) End Get End Property 

Then, no matter what you want on the main page, make sure that it is open, and you can access it through:

 Master.xxxx 
0
source

All Articles