I have a master page that contains elements for Ajax and certain standard controls that I do not wish to have to copy to every page on my site (hence the master!). However, these controls need a sub for an OnClick method. Now this method is edited differently on every page in the site. For example:

Sub link1_click
End Sub

asp:linkButton id="link1" onclick="link1_click"..

Sub link1_click
End Sub

I only do inline coding, so you know. I have currently tried this, which fails:
Public overridable sub..

Public Overloads Overrides Sub..

How do I make it work?

10 Years
Discussion Span
Last Post by SheSaidImaPregy

if i understand correctly, you want to override the default behaviour for the controls in your masterpage. You can do that as follows :

in your content pages, in the page load event ,attach eventhandler to your controls(you can remove the attached eventhandler first if you have already attached any in your master page)

protected void Page_Load(object sender,EventArgs e)
// Example, let's say you have a link button in your master page
LinkButton mine =(LinkButton)Master.FindControl("LinkButton1");
// removes the attached handler
mine.Click -= LinkButton1_Click;
// Adds the new handler
mine.Click += new EventHandler(mine_Click);
void mine_Click(object sender, EventArgs e)

Hope this works for you.

Edited by happygeek: fixed formatting


Thanks so far, but I have come to an error:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC32022: 'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Source Error:


Line 8:          Dim lbtnActMessages As LinkButton = CType(Master.FindControl("lbtnActMessages"), LinkButton)
Line 9:          Dim lbtnActivity As LinkButton = CType(Master.FindControl("lbtnActivity"), LinkButton)
Line 10:         lbtnActMessages.Click -= lbtnActMessages_Click
Line 11:         lbtnActMessages.Click += New EventHandler(lbtnActMessages_Click)

This is my Code in the content page:

Sub Page_Load(ByVal S As Object, ByVal E As EventArgs)
        Dim lbtnActMessages As LinkButton = CType(Master.FindControl("lbtnActMessages"), LinkButton)
        Dim lbtnActivity As LinkButton = CType(Master.FindControl("lbtnActivity"), LinkButton)
        lbtnActMessages.Click -= lbtnActMessages_Click
        lbtnActMessages.Click += New EventHandler(lbtnActMessages_Click)
        lbtnActivity.Click -= lbtnActivity_Click
        lbtnActivity.Click += New EventHandler(lbtnActivity_Click)
    End Sub
    Public Sub lbtnActMessages_Click(ByVal S As Object, ByVal E As EventArgs)
        'Dim inneraccount = Master.FindControl("inneraccount")
        'inneraccount.innerHTML = "1,031 Messages (0 New)<br />17 New Comments<br />Goto My Account"
    End Sub
    Public Sub lbtnActivity_Click(ByVal S As Object, ByVal E As EventArgs)
    End Sub

Also, this is my first time messing with Master Pages that has dynamic content within itself. How do I access the Div element within the master page as well? Thanks Serk.


Actually my code is in c#, i dont know the equivalent code in vb.net. You should learn about event registration techniques or syntax in vb.net(i know the c# ones). I tested the code before posting so it doesnt have any problem. I suggest that you work with c# instead of vb.net, since you are a javascript programmer, you will easily get accustomed to the syntax of c#. i have worked with vb.net but all is gone now :)


very useful information about working with masterpages programmatically is located at
And about accessing the div element, you should make it html server control first by adding the runat="server" attribute. Then you can access it the same way by using the FindControl method, but the casting will be different. Add the
using System.Web.UI.HtmlControls name space, then use HtmlGenericControl class name when casting. When you add the runat="server" attribute to normal html tags, they become html server controls and their class names are located in the System.Web.UI.HtmlControls namespace. You can check them out using the intellisense facility.


Yes I have made it a server control, however, accessing it like I normally would brings it up saying that is is not the right

I will try the code and look at the page.

I know the syntax would be easy for me to switch over to C#, however, I would still like to use vb.net instead. But thanks :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.