我在用户控件中有一个LinkButton,它处理了:
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 SubOn 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 SubThe 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);然后将以下内容调用到LinkButton 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 IfYou'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"
发布评论