好的,首先要做的是获得一次面板控制。其次,创建您的 click 方法,然后添加您的处理程序。这需要在页面加载事件之前完成。当您需要添加处理程序时,使用 page_Init 事件来创建您的按钮。
Private Sub WebForm1_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim tempPanel As Panel = form1.FindControl("upperpanel")
If tempPanel Is Nothing Then
Return
End If
For i = 1 To 500
Dim img As New ImageButton
img.ID = "ibtn" + i.ToString
img.ImageUrl = "images/design/click.gif"
AddHandler img.Click, AddressOf ImageButton_Click
tempPanel.Controls.Add(img)
Next
End Sub
Protected Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs)
Dim tempImageButton As ImageButton = CType(sender, ImageButton)
Select Case tempImageButton.ID
Case "ibtn1"
'blah
Case Else
End Select
End Sub
就性能而言,预先设置面板控制将提高性能。应该不多吧。创建 500 个按钮需要一些时间。