VB.NET中的onClick事件

时间:2010-10-07 20:55:12

标签: asp.net vb.net onclick

我在VB.NET中编程。 我正在尝试为div标签创建一个onClick事件。我怎么能在后面的代码中做到这一点?

2 个答案:

答案 0 :(得分:2)

一种可能的解决方案是创建一个带有Panel的Webusercontrol(将呈现为DIV)和一个不可见的Button(Display:none)。单击该按钮可以单击每个javascript按钮,这将导致自动回发。在Codebehind中,您将捕获ButtonClick-Event并引发自定义事件(DivClicked)。所以你可以在任何地方重用该控件。像这样:

<强> ClickableDiv.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ClickableDiv.ascx.vb" Inherits="WebApplication1.ClickableDiv" %>
<asp:Panel id="TheDiv" runat="server" onMouseOver="this.style.cursor='pointer'"  onclick="this.nextSibling.click()" /><asp:Button ID="DivButton" runat="server"  />

ClickableDiv.ascx Codebehind

Partial Public Class ClickableDiv
    Inherits System.Web.UI.UserControl

    Public Event DivClicked(ByVal src As ClickableDiv)

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.DivButton.Style.Add("display", "none")
    End Sub

    Public ReadOnly Property Div() As Panel
        Get
            Return Me.TheDiv
        End Get
    End Property

    Protected Sub DivButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DivButton.Click
        RaiseEvent DivClicked(Me)
    End Sub

End Class

例如在任何页面中(将其拖放到设计器中):

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim imgTest As New Image
        imgTest.ImageUrl = "http://skins.gmodules.com/ig/images/logos/approved/white.png"
        Me.ClickableDiv1.Div.Controls.Add(imgTest)
    End Sub

    Private Sub ClickableDiv1_DivClicked(ByVal src As ClickableDiv) Handles ClickableDiv1.DivClicked
        Dim div As ClickableDiv = src
    End Sub

顺便说一下,您还可以使用DIV onclick事件上的__doPostback-function来提升回发。

答案 1 :(得分:0)

1-创建隐藏的asp:按钮

2-在div上单击document.getElementById('<%=HiddenButton.ClientID%>').click();这将触发服务器上的button_click事件

**请记住为隐藏按钮设置UseSubmitBehaviour=false以使点击事件有效