开发者

Handle Click Event for LinkButton in User Control from Parent ASP.NET page

开发者 https://www.devze.com 2023-01-08 06:45 出处:网络
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

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 be开发者_运维百科hind page?

Update: Based on the answer, I have the updated 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.


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 


I think you should have a look into below code and links.

Parent Page aspx.cs part

public partial class getproductdetails : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
  Button btnYes = (Button)ucPrompt.FindControl("btnYes");
  btnYes.Click += new EventHandler(ucPrompt_btnYes_Click);
 }

 void ucPrompt_btnYes_Click(object sender, EventArgs e)
  {
    //Do Work
  }
}

Read more details how it works (Reference) :-

Handle events of usercontrol in parent page in asp.net

Calling a method of parent page from user control

0

精彩评论

暂无评论...
验证码 换一张
取 消