asp.net/VB.net:FindControl,而不是按ID按ControlType
我需要在asp.net应用程序的中继器中找到一个控件 目前我正在使用asp.net/VB.net:FindControl,而不是按ID按ControlType,asp.net,vb.net,types,repeater,findcontrol,Asp.net,Vb.net,Types,Repeater,Findcontrol,我需要在asp.net应用程序的中继器中找到一个控件 目前我正在使用FindControl(“IdOfControl”),这很好 但我需要按其类型找到控件(ImageButton) 我当前的代码: For Each rptItem As RepeaterItem In myRepeater.Items Dim imgBtn As ImageButton = TryCast(rptItem.FindControl("myImageBtn"), ImageButton) AddHan
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
有人能帮忙吗?试试这个
你的要求
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
我不确定您的中继器是否有嵌套控件,其中包含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
上述函数将在中继器控件中找到作为参数传入的第一个ImageButton。希望这有帮助 Sanjay Goswami发布了一个很好的解决方案 我不得不改变主意
If cn.[GetType]() = 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
这里有一个C#解决方案。谢谢这将引导我找到正确的代码。请检查我的答案,以获得完整的工作代码,并将此文章作为草稿。我想在表单中找到控件,并制作一个地图布局,这有助于实现此目的
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
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