2

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?

Keith L.
  • 2,084
  • 11
  • 41
  • 64
  • 1
    Here, there is a C# solution. http://programcsharp.com/blog/archive/2008/01/02/Recursively-find-controls-by-type-with-generics.aspx – Angelo Badellino Feb 17 '12 at 11:40

4 Answers4

5

Try this

your requirement

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
Sanjay Goswami
  • 1,386
  • 6
  • 13
  • thanks. This lead me to the correct code. check my answer for full working code with this post as draft. – Keith L. Feb 17 '12 at 12:03
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

I am not sure if your repeater will have nested controls, that contains ImageButtons. So, something along the lines of the following recursive code:

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

The above function will find the first ImageButton in the Repeater control that you have passed in as a parameter. Hope this helps.

skub
  • 2,266
  • 23
  • 33
0

Sanjay Goswami posted a good solution to work with.

I had to change the

If cn.[GetType]() = GetType(ImageButton) Then

to

If cn.GetType().Equals(GetType(ImageButton)) Then

and add my stuff.

Complete working code:

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
Keith L.
  • 2,084
  • 11
  • 41
  • 64