views:

94

answers:

1

I have a LinkButton within a User Control and it has handled with:

Private Sub LoginLinkLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkLinkButton.Click
        Response.Redirect("/", True)
End Sub

On certain ASPX pages I would like to handle the click from the page's code behind, opposed to within the user control. How do I override the handle within the control from the parent's code behind page?

Update: Based on the answer, I have the following no added to the User Control:

Public Event LoginLinkClicked As OnLoginClickHandler
Public Delegate Sub OnLoginClickHandler(ByVal sender As Object, ByVal e As EventArgs)

[...]

Private Sub LoginLinkLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginLinkLinkButton.Click
        **If OnLoginClickHandler IsNot Nothing Then**
            RaiseEvent LoginLinkClicked(Me, e)
        Else
            Response.Redirect("/", True)
        End If
End Sub

The problem is determining the correct syntax for the if line because the above is invalid.

+1  A: 

You'll have to expose a new event from the user control. Apologies as the following code is all in C# and it's been a long time since I touched VB.Net, but you should get the idea:

You can use a delegate event by adding the following to your UserControl:

public event OnLoginClickHandler LoginClick;
public delegate void OnLoginClickHandler (object sender, EventArgs e);

Then call the following to your LinkButton Click event:

protected void LoginLinkLinkButton_Click(object sender, EventArgs e)
{
    // Only fire the event if there's a subscriber
    if (OnLoginClickHandler != null) 
    {
        OnLoginClickHandler(sender, e); 
    }
    else
    {
        // Not handled, so perform the standard redirect
        Response.Redirect("/", true);
    }
}

You can then just hook up into this within your aspx markup:

<asp:LinkButton runat="server" ID="Foo" OnLoginClick="Foo_LoginClick" />

And your server side event handler on your Page will be as follows:

protected void Foo_LoginClick_Click(object sender, EventArgs e)
{
    // This event was fired from the UserControl
}

UPDATE

I think this is how you translate the event subscription check to VB.Net:

If LoginClick IsNot Nothing Then
    RaiseEvent LoginClick(sender, e) 
End If 
GenericTypeTea
Good solution. I'm having trouble translating if (OnLoginClickHandler != null) { OnLoginClickHandler(sender, e); } I'll post what I have in the question.
Josh
That creates the following error: 'OnLoginClickHandler' is a type and cannot be used as an expression.
Josh
@Josh - I re-updated it, my mistake!
GenericTypeTea
I tried that as well and received this error: 'Public Event LoginClick(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Josh
@Josh - Updated again. I missed the `RaiseEvent` call.
GenericTypeTea
The problem is that you cannot test LoginClick for Nothing. Based on research it looks like VB.NET doesn't expose the subscribers as easily C#. Being crunched for time I had implement without a default. I'll mark this as answered because you answered the main question about how to call an event from the parent page. Thanks for your help.
Josh
@Josh - It should work. See this example here: http://www.cynotwhynot.com/blog/post/Dont-Blindly-Raise-Events-in-VBNET.aspx
GenericTypeTea