从父ASP.NET页面处理用户控件中的LinkButton的Click事件(Handle Click Event for LinkButton in User Control from Parent

编程入门 行业动态 更新时间:2024-10-25 05:27:43
从父ASP.NET页面处理用户控件中的LinkButton的Click事件(Handle Click Event for LinkButton in User Control from Parent ASP.NET page)

我在用户控件中有一个LinkBut​​ton,它处理了:

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

在某些ASPX页面上,我想处理页面代码后面的点击,与用户控件内部相反。 如何从父页面后面的代码覆盖控件中的句柄?

更新:根据答案,我更新了用户控件:

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

问题是确定if行的正确语法,因为上面的内容无效。

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

最满意答案

您必须从用户控件中公开新事件。 道歉如下面的代码全部都在C#中 ,自从我触及VB.Net以来已经很久了,但你应该明白这个想法:

您可以通过将以下内容添加到UserControl来使用委托事件:

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

然后将以下内容调用到LinkBut​​ton Click事件:

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); } }

然后,您可以在aspx标记中连接到这个:

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

页面上的服务器端事件处理程序如下:

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

UPDATE

我认为这是您将事件订阅检查转换为VB.Net的方式:

If LoginClick IsNot Nothing Then RaiseEvent LoginClick(sender, e) End If

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

更多推荐

Sub,End,用户,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-30 01:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   事件   页面   用户   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!