简体   繁体   中英

Visual Basic looping controls

I have a web application that I'm writing. I have a lot of buttons, all named the same thing except with a number appended (button1,button2,button3...etc.). All of these are in a panel named Panel3. I would like to loop through these blank ImageButtons and set their image using .ImageUrl. I know my getIconImage() function works.

    Dim cntrl As Control
    For Each cntrl In Me.Panel3.Controls

        cntrl.ImageUrl = getIconImage(4)

    Next

The problem is "cntrl" does not recognize .ImageUrl as an option. It is like it doesn't recognize itself as an ImageButton. I am using asp.net and vb.net. Thanks!

(as a note: I have also tried "Me.Controls" with no such luck. Also, I have tried setting a temporary ImageButton object equal to "cntrl" and modifying the .ImageUrl from there.)

You don't say what the issue is so i'll assume you need to check which type of control you are getting in the loop:

 Dim cntrl As Control
 For Each cntrl In Me.Panel3.Controls
    if TypeOf cntrl Is ImageButton Then 
       cntrl.ImageUrl = getIconImage(4)
    End if
 Next

Try this:

For Each cntrl As ImageButton In Me.Panel3.Controls.OfType(Of ImageButton)

May have to do Imports System.Linq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM