4

I have a master page where in I have a link button. In one of the content pages, I need to hide the linkbutton and replace with image button. The click event handler for image button should exactly do the same thing as the click event of the link button in master page. So is there a way of calling the Master page link button click event handler from the Image button click event handler in Content page?

I have researched and there seem to be a lot in case Master page wants to call the event handler in Content page...

Any inout will be highly appreciated.

Thanks

Sugar Bowl
  • 1,722
  • 1
  • 19
  • 24

3 Answers3

7

In 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#.

I'm looking into calling the master pages event handler more directly

Jon P
  • 19,442
  • 8
  • 49
  • 72
  • Thanks for the reply... yeah this is what i was looking for... I was calling other Master page methods from content page by making them public...how come i didnt think about making events public too...Sorry I am new at SF..so I cant give u credit for this answer – Sugar Bowl May 19 '11 at 16:44
  • 1
    No problem, just don't forget to when you get enough reputation, though you probably do have enough rep to accept by clicking the tick, just not enough to upvote. I COuld be wrong though :-) – Jon P May 19 '11 at 22:16
4

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

The result is as stated strongly typed and there is no need to cast.

Example usage:

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

Using this directive actually produces the same code as @Scott's implementation but does not require you to know the type of the masterpage.

You can then start using your masterpage by lets say:

Master.Title = "My Page Title";

You will be able to invoke events from the master this way as well. Use Master.FindControl to find the master control you want.

Example of find control:

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

BUT I would suggest using the OnClick property of the ImageButton and set it to a publicly accessible void/Sub on the Master page. Then simply call that void/Sub like:

Master.ImageButtonClick();
Jeremy
  • 3,880
  • 3
  • 35
  • 42
0

If your Master Page is MyMaster, make a property like this:

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

Then whatever you want to access in the Master Page, make sure it's Public, and you will be able to access it via:

Master.xxxx
Code Maverick
  • 20,171
  • 12
  • 62
  • 114