如何在动态按钮 - VB.net上创建动态按钮点击事件 [英] How can i create dynamic button click event on dynamic button - VB.net
本文介绍了如何在动态按钮 - VB.net上创建动态按钮点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码:
受保护的子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.net & asp.net?
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 Sub
Load 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 Sub
Event 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 Sub
Problem:
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_Click
Then EventHandler
must be at least Protected
, but your is Private
- it cannot be accessed then
这篇关于如何在动态按钮 - VB.net上创建动态按钮点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文