Dynamic Button Rollovers - Imagebuttons in WebControls

Discussion in 'ASP .Net Web Controls' started by crferguson@gmail.com, Jul 18, 2007.

  1. Guest

    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
    ========================
     
    , Jul 18, 2007
    #1
    1. Advertising

  2. I've created a free RolloverImageButton that encapsulates similar logic:
    http://SteveOrr.net/articles/InheritAndExtend.aspx
    This should simplify things even further.

    --
    I hope this helps,
    Steve C. Orr,
    MCSD, MVP, CSM, ASPInsider
    http://SteveOrr.net



    <> wrote in message
    news:...
    >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
    > ========================
    >
     
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Jul 25, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    0
    Views:
    338
    Nedu N
    Aug 13, 2003
  2. RA
    Replies:
    2
    Views:
    2,605
  3. Frances Del Rio

    forms: img submit button: can u do rollovers??

    Frances Del Rio, Sep 15, 2004, in forum: Javascript
    Replies:
    2
    Views:
    135
    Frances Del Rio
    Sep 15, 2004
  4. John Ortt
    Replies:
    0
    Views:
    132
    John Ortt
    Nov 25, 2004
  5. John Ortt
    Replies:
    3
    Views:
    143
    Michael Winter
    Nov 30, 2004
Loading...

Share This Page