2012-02-17 2 views
0

Мне нужно найти элемент управления в ретрансляторе в моем приложении asp.net.asp.net/VB.net: FindControl вместо ID с помощью ControlType

На данный момент я использую 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 

Может кто-нибудь помочь?

+1

Здесь есть C# решение. http://programcsharp.com/blog/archive/2008/01/02/Recursively-find-controls-by-type-with-generics.aspx – AngeloBad

ответ

3

Попробуйте

ваше требование

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 
+0

спасибо. Это привело меня к правильному коду. проверьте мой ответ на полный рабочий код с этой записью как черновик. –

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 

выше функция будет найти первый ImageButton в элементе управления Repeater, что вы переданная в качестве параметра. Надеюсь это поможет.

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 
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 
+0

Я хотел найти элементы управления в форме и сделать макет карты, которая помогла этому пути –