asp.net/VB.net: FindControl, instead of by ID by ControlType

Keith L. picture Keith L. · Feb 17, 2012 · Viewed 7k times · Source

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?

Answer

Sanjay Goswami picture Sanjay Goswami · Feb 17, 2012

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