asp.net/VB.net:FindControl,而不是按 ID 按 ControlType
我需要在我的 ASP.NET 应用程序中找到中继器中的控件。
目前我正在使用 FindControl("IdOfControl") ,效果很好。
但我需要按其类型找到一个控件(ImageButton
)。
我当前的代码:
For Each rptItem As RepeaterItem In myRepeater.Items
Dim imgBtn As ImageButton = TryCast(rptItem.FindControl("myImageBtn"), ImageButton)
AddHandler imgBtn.Click, AddressOf imgBtn_Click
Next
我正在寻找类似的东西:
For Each rptItem As RepeaterItem In myRepeater.Items
Dim imgBtn As ImageButton = TryCast(rptItem.FindControl(TypeOf ImageButton), ImageButton)
AddHandler imgBtn.Click, AddressOf imgBtn_Click
Next
有人可以帮忙吗?
i need to find a control in a repeater in my asp.net application.
At the moment I'm using FindControl("IdOfControl")
and this is working well.
But I need to find a control by its type (ImageButton
).
My current code:
For Each rptItem As RepeaterItem In myRepeater.Items
Dim imgBtn As ImageButton = TryCast(rptItem.FindControl("myImageBtn"), ImageButton)
AddHandler imgBtn.Click, AddressOf imgBtn_Click
Next
I'm searching for something like that:
For Each rptItem As RepeaterItem In myRepeater.Items
Dim imgBtn As ImageButton = TryCast(rptItem.FindControl(TypeOf ImageButton), ImageButton)
AddHandler imgBtn.Click, AddressOf imgBtn_Click
Next
Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
你的要求
Try this
your requirement
我不确定您的中继器是否有包含图像按钮的嵌套控件。因此,类似于以下递归代码:
上面的函数将找到您作为参数传入的 Repeater 控件中的第一个 ImageButton。希望这有帮助。
I am not sure if your repeater will have nested controls, that contains ImageButtons. So, something along the lines of the following recursive code:
The above function will find the first ImageButton in the Repeater control that you have passed in as a parameter. Hope this helps.
Sanjay Goswami 发布了一个很好的解决方案。
我必须更改
并
添加我的东西。
完整的工作代码:
Sanjay Goswami posted a good solution to work with.
I had to change the
to
and add my stuff.
Complete working code: