Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > image button rollover

Reply
Thread Tools

image button rollover

 
 
smokeyd
Guest
Posts: n/a
 
      03-14-2007
i am trying to create a simple image button rollover.. i have
searched this forum and found a number of solutions but none seem to
work. i am just trying to get the onmouseover to swap the image then
restore when rolled out on a web page in vb.net. please can someone
help?

 
Reply With Quote
 
 
 
 
Mike Hofer
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 6:25 am, "smokeyd" <(E-Mail Removed)> wrote:
> i am trying to create a simple image button rollover.. i have
> searched this forum and found a number of solutions but none seem to
> work. i am just trying to get the onmouseover to swap the image then
> restore when rolled out on a web page in vb.net. please can someone
> help?


I did it on the server side, while I was setting up the page. Most of
my pages have an OK button and a Cancel button. For consistency's
sake, the images are all named the same. That is, the standard buttons
end in ".jpg" and the hover style ends in "_hover.jpg".

We use ImageButton controls to represent buttons so that we can
respond to click events server-side. When I'm designing the form, I
always only set the ImageUrl property, and I always only set it to the
*unhovered* image.

I wrote a method in the page's base class called SetButtonImages, that
looks something like this:

Protected Sub SetButtonImages(ByVal button As ImageButton)

Dim imageUrl As String

imageUrl = button.imageUrl

' Set the image that appears when the user moves the mouse off
' of the button.
image.Attributes.Add("onmouseout", "src=""" & imageUrl & """")

' Remove the .jpg extension, and add the "_hover.jpg" extension
imageUrl = imageUrl.SubString(0, imageUrl.Length - 4)
imageUrl &= "_hover.jpg"

' Set the image that appears when the user hovers the mouse
' over the image button.
button.Attributes.Add("onmouseover", "src=""" & imageUrl & """")

End Sub

(WARNING: I'm recalling this from memory, since the boss just tweaked
our firewall, and I can't peak at the source code to refresh my
memory, so I'm sorry if there are bugs.)

The method is invoked in the derived pages, like this:

Protected Sub PageLoad(ByVal sender As Object, ByVal e As EventArgs)
Handles Page.Load
...
SetButtonImages(btnOk)
SetButtonImages(btnCancel)
End Sub

This implementation saved me a lot of time, but it's based on a base
class for your pages. If you're not using a base class, you can simply
put the SetButtonImages function in a sepraate class, or even in a
module and invoke it from there (so that you don't have to repeat it
in every page). Once you have that method working the way you want it
to, it becomes highly reusable. You can change the file extension to
match whatever image types or file names you're using.

The caveat, of course, is that you have a consistent image naming
scheme. You can, of course, change the arguments to the method to pass
in the names of the images. That simplifies the entire task, and is
likely far more reliable if your file names aren't consistent. (A lack
of consistency doesn't mean anything--it's a frequent phenomenon, and
just indicates that the images likely came from disparate sources.)

I hope this helps!

Mike

 
Reply With Quote
 
 
 
 
smokeyd
Guest
Posts: n/a
 
      03-14-2007
On 14 Mar, 10:58, "Mike Hofer" <(E-Mail Removed)> wrote:
> On Mar 14, 6:25 am, "smokeyd" <(E-Mail Removed)> wrote:
>
> > i am trying to create a simple image button rollover.. i have
> > searched this forum and found a number of solutions but none seem to
> > work. i am just trying to get the onmouseover to swap the image then
> > restore when rolled out on a web page in vb.net. please can someone
> > help?

>
> I did it on the server side, while I was setting up the page. Most of
> my pages have an OK button and a Cancel button. For consistency's
> sake, the images are all named the same. That is, the standard buttons
> end in ".jpg" and the hover style ends in "_hover.jpg".
>
> We use ImageButton controls to represent buttons so that we can
> respond to click events server-side. When I'm designing the form, I
> always only set the ImageUrl property, and I always only set it to the
> *unhovered* image.
>
> I wrote a method in the page's base class called SetButtonImages, that
> looks something like this:
>
> Protected Sub SetButtonImages(ByVal button As ImageButton)
>
> Dim imageUrl As String
>
> imageUrl = button.imageUrl
>
> ' Set the image that appears when the user moves the mouse off
> ' of the button.
> image.Attributes.Add("onmouseout", "src=""" & imageUrl & """")
>
> ' Remove the .jpg extension, and add the "_hover.jpg" extension
> imageUrl = imageUrl.SubString(0, imageUrl.Length - 4)
> imageUrl &= "_hover.jpg"
>
> ' Set the image that appears when the user hovers the mouse
> ' over the image button.
> button.Attributes.Add("onmouseover", "src=""" & imageUrl & """")
>
> End Sub
>
> (WARNING: I'm recalling this from memory, since the boss just tweaked
> our firewall, and I can't peak at the source code to refresh my
> memory, so I'm sorry if there are bugs.)
>
> The method is invoked in the derived pages, like this:
>
> Protected Sub PageLoad(ByVal sender As Object, ByVal e As EventArgs)
> Handles Page.Load
> ...
> SetButtonImages(btnOk)
> SetButtonImages(btnCancel)
> End Sub
>
> This implementation saved me a lot of time, but it's based on a base
> class for your pages. If you're not using a base class, you can simply
> put the SetButtonImages function in a sepraate class, or even in a
> module and invoke it from there (so that you don't have to repeat it
> in every page). Once you have that method working the way you want it
> to, it becomes highly reusable. You can change the file extension to
> match whatever image types or file names you're using.
>
> The caveat, of course, is that you have a consistent image naming
> scheme. You can, of course, change the arguments to the method to pass
> in the names of the images. That simplifies the entire task, and is
> likely far more reliable if your file names aren't consistent. (A lack
> of consistency doesn't mean anything--it's a frequent phenomenon, and
> just indicates that the images likely came from disparate sources.)
>
> I hope this helps!
>
> Mike


works a treat!

thanks very much for that mike, it works great. cant beleive it is so
difficult in .net though!

many thanks for your help.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS Button Rollover Effects Blake ASP .Net 3 05-18-2011 02:46 PM
rollover image (but image is a php document) Dan Javascript 3 05-05-2007 07:46 AM
Intermittent image display problem within 179 image rollover Mark Szlazak Javascript 3 04-25-2006 06:32 PM
Login "rollover" button question VB Programmer ASP .Net 0 06-09-2004 12:54 PM



Advertisments