如何在动态按钮

编程入门 行业动态 更新时间:2024-10-18 01:41:28
本文介绍了如何在动态按钮 - 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 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

更多推荐

如何在动态按钮

本文发布于:2023-11-02 14:20:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   动态   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!