我需要在我的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
有人可以帮忙吗?
答案 0 :(得分:4)
试试这个
您的要求
For Each ri As RepeaterItem In myRepeater.Items
For Each cn As Control In ri.Controls
If cn.[GetType]() = GetType(ImageButton) Then
Response.Write("ss")
Response.Write(vbLf)
End If
Next
Next
e.g
For Each cn As Control In form1.Controls
If cn.[GetType]() = GetType(ImageButton) Then
Response.Write("ss")
End If
Next
答案 1 :(得分:0)
我不确定您的转发器是否具有包含ImageButtons的嵌套控件。所以,下面的递归代码:
Public Function FindControl(ByVal ParentControl As Control) As Control
Dim ReturnedControl As New Control
For Each CurrentControl As Control In ParentControl.Controls
CurrentControl.[GetType]() = GetType(ImageButton) Then
ReturnedControl = CurrentControl
Exit For
End If
If (CurrentControl.HasControls) Then
ReturnedControl = FindControl(CurrentControl)
End If
Next
Return ReturnedControl
End Function
上面的函数将找到您作为参数传入的Repeater控件中的第一个ImageButton。希望这可以帮助。
答案 2 :(得分:0)
Sanjay Goswami发布了一个很好的解决方案。
我不得不改变
If cn.[GetType]() = GetType(ImageButton) Then
到
If cn.GetType().Equals(GetType(ImageButton)) Then
并添加我的东西。
完整的工作代码:
For Each rptItem As RepeaterItem In myRepeater.Items
For Each cn As Control In rptItem.Controls
If cn.GetType().Equals(GetType(ImageButton)) Then
AddHandler (TryCast(rptItem.FindControl(cn.ID), ImageButton)).Click, AddressOf imgBtn_Click
End If
Next
Next
答案 3 :(得分:0)
For Each cn As Control In Me.Controls
If (cn.[GetType]().Equals(GetType(Button))) Then
Dim str1 As String = cn.Text
ds = fobj.getrecord("select shopid from tbstallbooking where shopid='" + str1 + "'")
n = ds.Tables(0).Rows.Count
If (n > 0) Then
cn.BackColor = Color.Red
cn.Enabled = False
ds.Clear()
Else
cn.BackColor = Color.Green
cn.Enabled = True
End If
End If
Next