动态添加的linkbutton的处理程序没有被调用 [英] handler of dynamically added linkbutton is not getting called
本文介绍了动态添加的linkbutton的处理程序没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
在我的项目中,当我遍历数据库表时,我正在动态创建一些链接按钮.我正在创建按钮并添加事件处理程序,如下所示
Hello All,
In my Project i am creating some linkbuttons dynamically when i iterate through my DB table. I am creating the buttons and adding event handler as follows
'Iterating through the Table
If (ds.Tables(0).Rows.Count > 0) Then
'Reading row by row from DB
Dim ros As Int32 = ds.Tables(0).Rows.Count - 1
Do While ros >= 0
Dim tr As TableRow = New TableRow()
Dim tc As TableCell = New TableCell()
Dim btnUser As LinkButton = New LinkButton()
AddHandler btnUser.Click, AddressOf btnUser_Click
btnUser.ID = (ds.Tables(0).Rows(ros)("UserName")).ToString
tc.Controls.Add(btnUser)
tr.Cells.Add(tc)
tbl.Rows.Add(tr)
ros = ros - 1
Loop
End If
我给的事件处理程序如下
The event handler i have given is as follows
Private Sub btnUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'setting the session value
Dim btn As LinkButton = sender
Dim newsId As String = btn.ID.ToString
Session("SearchedUserName") = newsId
End Sub
但是在运行时单击链接按钮时,未调用处理程序函数.我无法理解我在哪里犯了错误.有人可以帮我这个忙.
But while clicking on the linkbutton at runtime,the handler function is not getting called. I am not able to understand where i made mistake. Anybody help me in this please.
推荐答案
该代码在生命周期中发生了什么事件?我相信您必须在Init
或PreInit
页面事件中动态添加控件.
将控件添加到这些事件中的任何一个中,然后只有您才能处理click事件.
试试这个:
http://www.aspsnippets.com/Articles/在ASP.Net.aspx中创建动态按钮链接按钮和图像按钮 [
What event in the lifecycle is that code in? You have to add controls dynamically, I believe, in theInit
orPreInit
page events.
Add your controls in either of these events then only you''ll able to handle click event.
Try this:
http://www.aspsnippets.com/Articles/Creating-Dynamic-Button-LinkButton-and-ImageButton-in-ASP.Net.aspx[^]
--Amit
检查此
不触发动态创建按钮的单击事件 [ ^ ]
最好的问候
米特瓦里(M.Mitwalli)
Hi ,
Check this
not firing dynamically cretated button click event[^]
Best Regards
M.Mitwalli
这篇关于动态添加的linkbutton的处理程序没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文