Dynamic Button Rollovers - Imagebuttons in WebControls

C

crferguson

I just wanted to share a solution I found to performing a dynamic
image swap on a group of imagebuttons in a webcontrol. The scenario
is I have a webcontrol with nothing but a table containing one column
of imagebuttons in it. This wil be a typical button column for a
vertical menu like on the left side of this site: http://www.nsr-inc.com

This code, when set to run in Form_Load, figures out the names of the
images to use in the image swap and then adds the onmouseover /
onmouseout attributes dynamically. Then, if you add or remove buttons
in the future, so long as you stick to the naming convention needed by
the code, you won't have to re-code each button to use specific
images.

The image naming convention to use for the button images should be
whatevernameyouwant + 01.gif and whatevernameyouwant + 02.gif (ex:
buttonup01.gif, buttondown02.gif). Next, make sure the "src='../
images/" part is set to wherever you're keeping your button images
relative to the web control. Last, add the AddAttributes call in the
Form_Load sub.

=================

Private Sub AddAttributes()
Dim sImage As String

For Each ctl As Control In Me.Controls
If TypeOf ctl Is ImageButton Then
sImage = GetImageName(DirectCast(ctl,
ImageButton).ImageUrl)

DirectCast(ctl,
ImageButton).Attributes.Add("onmouseover", "src='../images/" & sImage
& "02.gif'")
DirectCast(ctl,
ImageButton).Attributes.Add("onmouseout", "src='../images/" & sImage &
"01.gif'")
End If
Next
End Sub

Private Function GetImageName(ByVal ImageURL As String) As String
Dim intX As Integer = InStr(ImageURL, "/")
GetImageName = ImageURL
Do Until InStr(GetImageName, "/") = 0
GetImageName = Mid(GetImageName, InStr(GetImageName, "/")
+ 1)
Loop
GetImageName = Mid(GetImageName, 1, Len(GetImageName) - 6)
End Function
========================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top