<asp:ImageButton
ID="a"
runat="server"
/>
<label
id="lb"
for="a"
onclick="这里是事件"
runat="server">点这里也可以</label>
至于onclick事件在哪里加都行,在页面加的话就如上,直接在label里加就行了,如果想在.cs后台文件添加的话就如下
lb.Attributes.Add("onclick","alert(\"显示的测试文字\")")
两种方法:
1. 用WithEvents声明变量,这样你就可以像对待拖上去的控件一样 *** 作:
Public Class Form3
WithEvents btn As New Button
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
MsgBox(Now)
End Sub
End Class
2. 使用AddHandler 语句:
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim btn As New Button
AddHandler btn.Click, AddressOf btn_Click
Controls.Add(btn)
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
MsgBox(Now)
End Sub
End Class
WithEvents (Visual Basic):http://msdn.microsoft.com/zh-cn/library/vstudio/aty3352y
AddHandler 语句:http://msdn.microsoft.com/zh-cn/library/vstudio/7taxzxka.aspx
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)