-1
我正在页面上动态创建一个按钮。现在我想使用该按钮上的按钮单击事件。我怎样才能做到这一点在VB.net & asp.net?如何在动态按钮上创建动态按钮点击事件 - VB.net
我的代码:
页面加载:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
LoadControls()
Catch ex As Exception
End Try
End Sub
负载控制
Private Sub LoadControls()
Try
Dim ButtonTable As New HtmlTable
Dim ButtonTableRow As New HtmlTableRow
Dim ButtonTableCell As New HtmlTableCell
Dim btnCode As New Button
btnCode.ID = "btnCode"
btnCode.Text = "btnCode"
AddHandler btnCode.Click, AddressOf btnCode_Click
ButtonTableCell.Controls.Add(btnCode)
ButtonTableRow.Cells.Add(ButtonTableCell)
ButtonTable.Rows.Add(ButtonTableRow)
ControlsPlaceHolder.Controls.Add(ButtonTable)
Catch ex As Exception
End Try
End Sub
事件处理程序
Private Sub btnCode_Click(sender As Object, e As EventArgs)
Dim buttonId As New Button
Try
buttonId = DirectCast(sender, Button)
// My execution
Catch ex As Exception
End Try
End Sub
问题:
事件处理程序不会出现.. !!它抛出一个错误
Multiple controls with the same ID were found
什么不对这个代码.. !!
有你看过我的代码?我在代码中有这部分代码,但它不起作用 –
你有一个用'AddHandler ...'行检查事件处理程序的名字的错字 – Fabio
不,这不是问题在这里,发布错误时发布它在这里.. !! –