Solved

MouseOver Image in Master Pages

Posted on 2007-11-16
5
686 Views
Last Modified: 2012-05-05
Hi Experts !
I need some help please with using MouseOver on some images in my MasterPage
I have a button that I want to replace with different image when I do OnMouseOver and OnMouseOut.
Simple , right ?
So I did:
<asp:ImageButton ID="btnWeddings" runat="server" ImageUrl="~/Images/weddingBtn.gif" onmouseover="this.src='Images/weddingPressedBtn.gif';" onmouseout="this.src='Images/weddingBtn.gif';" OnClick="ImageButton4_Click"/>
It works as long as I stay on the same page.

Here is a problem... As soon as the MasterPage pulls content from another page OnMouseOver and OnMouseOut images break.
I tried using:
onmouseover="this.src='~/Images/weddingPressedBtn.gif';"
and
onmouseover="this.src='../Images/weddingPressedBtn.gif';"
But ~/ doesn't seem to work with events like OnMouseOver for some reason.
I also tried adding to the page load event:
        btnWeddings.Attributes.Add("OnMouseOver","this.src='~/Images/weddingPressedBtn.gif'");
It only works with direct path such as Images/.... breaking the whole thing if I pull content from another page.
Thank you in advance !
0
Comment
Question by:techsuppoprt
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:craskin
Comment Utility
onmouseover="this.src='~/Images/weddingPressedBtn.gif';"

since this is essentially javascript, i don't think the ~ is going to act like it does in a purely .net postback event. if you place the entire url there, does it work? like this.src='http://mysite.com/images/...gif
0
 
LVL 6

Accepted Solution

by:
ventaur earned 500 total points
Comment Utility
You almost had it with your code approach, but since onMouseOver is a client-side event, the "~" won't get translated correctly without some help. See below.
btnWeddings.Attributes.Add("onMouseOver", string.Format("this.src='{0}'", ResolveClientUrl("~/Images/weddingPressedBtn.gif")));

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
Thanks your answers guys! Craskin, sorry using full URLs is not an option although I'm sure it would work.

Ventaur, it worked like a charm. Do you know of any good guide on the Internet where I can read about this ? Being new I want to understand what I'm doing... Not just copy/paste the code :)  I added points.
0
 
LVL 6

Expert Comment

by:ventaur
Comment Utility
Here are a couple of articles on paths in ASP.NET:
http://msdn2.microsoft.com/en-us/library/ms178116.aspx
http://www.aspnetpro.com/newsletterarticle/2003/11/asp200311kd_l/asp200311kd_l.asp

How to use client-side events in server-side controls:
http://support.microsoft.com/kb/318814/es

Injecting client-side code in ASP.NET (this one is more advanced for emitting JS code that you do not want to duplicate on the page, etc.):
http://msdn2.microsoft.com/en-us/library/aa478975.aspx

Resolving URLs:
http://www.pluralsight.com/blogs/fritz/archive/2006/02/06/18596.aspx
http://clipmarks.com/clipmark/6C2F8613-F6CA-4504-8BE1-FB23AC4D6C17/


Enjoy coding!
0
 
LVL 1

Author Closing Comment

by:techsuppoprt
Comment Utility
Thank you!
As always ExpertSexChange saves the day :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now