本文介绍了如何在动态按钮 - VB上创建动态按钮点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在页面上动态创建一个按钮。现在我想在该按钮上使用按钮点击事件。我如何在VB和asp?
我的代码:
受保护的子Page_Load(发件人作为对象,e作为EventArgs)处理Me.Load 尝试 LoadControls() Catch ex As Exception 结束尝试 End Sub
加载控件
Private Sub LoadControls()尝试 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) 控制sPlaceHolder.Controls.Add(ButtonTable) Catch ex As Exception 结束尝试 End Sub事件处理程序
Private Sub btnCode_Click对象,e作为EventArgs) Dim buttonId作为新按钮尝试 buttonId = DirectCast(sender,Button) //我的执行 Catch ex As Exception End尝试 End Sub问题:
事件处理程序不出现..它发出错误
找到具有相同ID的多个控件
这个代码有什么问题.. !!
解决方案创建动态按钮订阅您的事件处理者
AddHandler btnCode.Click,AddressOf btnCode_Click然后 EventHandler 必须至少为受保护,但您是私人 - 无法访问
I am creating a button on page dynamically. now i want to use button click event on that button. How can i do this in VB & asp?
My code:
Page Load:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Try LoadControls() Catch ex As Exception End Try End SubLoad Controls
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 SubEvent Handler
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 SubProblem:
event handler doesn't arises..!! it throws an error
Multiple controls with the same ID were found
What is wrong with this code..!!
解决方案After creating a dynamic button "subscribe" your eventhandler btnCode_Click to event of the button:
AddHandler btnCode.Click, AddressOf btnCode_ClickThen EventHandler must be at least Protected, but your is Private - it cannot be accessed then
更多推荐
如何在动态按钮
发布评论