Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2127
  • Last Modified:

How to make mouse-over on hyperlink with image url

Hi Experts

I have several hyperlinks using an Image Url.
Is it possible to have a mouse-over effect - like a:hover on this?

Exempel code:
<asp:HyperLink ImageUrl="images/knapp_lesmer.png" ID="lnkLesmer" runat="server" Text="Les mer" NavigateUrl='<%# Eval ("VareID","varedetalj.aspx?Vare={0}") %>'></asp:HyperLink>

Thank you
0
Raggdog
Asked:
Raggdog
  • 14
  • 13
1 Solution
 
prairiedogCommented:
What exact effect do you need?
0
 
RaggdogAuthor Commented:
I want the image to switch to another
0
 
RaggdogAuthor Commented:
I want the image to switch to "images/knapp_lesmer_over.png" on mouse-over...
Is this doooable?
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
prairiedogCommented:
Here you go:
http://ecybers.com/webdesign/javascriptonmouse.htm

Let me know if you have trouble undertand or implement this JavaScript example.
0
 
RaggdogAuthor Commented:
well, maybe I do all wrong, but I does not seem like I can write 'onMouseOver=....' inside a <asp:Hyperlink>
This exampel is for a href?
0
 
prairiedogCommented:
OK, in your Page_Load event handler, add this line:
this.lnkLesmer.Attributes.Add("OnMouseOver", "this.firstChild.src='" & newImagePath & "'");

If you want to change the image back to the original when user move away their mouse, then you need to add this line after the above line:
this.lnkLesmer.Attributes.Add("OnMouseOut", "this.firstChild.src='" & origianlImagePath & "'");
0
 
RaggdogAuthor Commented:
I'm sorry for my ignorance, but where in this do I write the name of the images? Both the original ; knapp_lesmer.png and the mouseover; knapp_lesmer_over.png?

This is very new to me - sorry...
Thank you
0
 
prairiedogCommented:
It's ok. Say you store all your images under a folder called "Images" in your web application, you can change those two lines of code as follows:
this.lnkLesmer.Attributes.Add("OnMouseOver", "this.firstChild.src='" + Request.ApplicationPath() +
                      "/Images/knapp_lesmer_over.png'");

this.lnkLesmer.Attributes.Add("OnMouseOut", "this.firstChild.src='" + Request.ApplicationPath() +
                      "/Images/knapp_lesmer.png'");

0
 
RaggdogAuthor Commented:
Thank you for you patience 'prairiedog'. I did what you said, and I got this error:

Compiler Error CS0117
butikk does not contain a definition for 'lnkLesmer'

Am I missing something?
0
 
prairiedogCommented:
'lnkLesmer' is your HyperLink's ID. I copied it from your example code.
0
 
RaggdogAuthor Commented:
Yes I know, and I have double checked it and that is correct.
Why does I get this error then?
0
 
prairiedogCommented:
Where did you put those two lines? Post your code please, including your aspx page.
0
 
RaggdogAuthor Commented:
Here is the hole mess...:)

Thank you again
//This is .cs
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class butikk : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.lnkLesmer.Attributes.Add("OnMouseOver", "this.firstChild.src='" + Request.ApplicationPath() +
                      "/grafikk/knapp_lesmer_over.png'");
        this.lnkLesmer.Attributes.Add("OnMouseOut", "this.firstChild.src='" + Request.ApplicationPath() +
                      "/grafikk/knapp_lesmer.png'");
    }
}
 
 
//this is butikk.aspx
 
<%@ Page Language="C#" MasterPageFile="~/HovedPage.master" AutoEventWireup="true" CodeFile="butikk.aspx.cs" Inherits="butikk" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
        <asp:Repeater ID="rprKategori" runat="server" DataSourceID="SqlDataSource_rprKategoriVarer">
        <ItemTemplate>
        <div id="Kategori_item">
        <table border="0" cellspacing="0" cellpadding="8">
          <tr>
            <td class="kategoribilde" rowspan="3"><asp:Image Width="95px" runat="server" ID="imgVare" ImageUrl='<%# Eval ("hovedbilde","bilder/{0}") %>' /></td>
            <td colspan="2"><h4><asp:Label ID="lblTittel" runat="server" Text='<%# Eval("overskrift") %>'></asp:Label></h4></td>
          </tr>
          <tr>
            <td colspan="2"><asp:Label ID="lblBeskrivelse" runat="server" Text='<%# Eval ("varebeskrivelse") %>'></asp:Label>
            <asp:HyperLink ImageUrl="grafikk/knapp_lesmer.png" ID="lnkLesmer" runat="server" Text="Les mer" NavigateUrl='<%# Eval ("VareID","varedetalj.aspx?Vare={0}") %>'></asp:HyperLink><br /><br /></td>
          </tr>
          <tr>
            <td class="kategorilink">Kr <asp:Label ID="lblPris" runat="server" Text='<%# Eval("pris","{0:N2}") %>'></asp:Label></td>
            <td></td>
          </tr>
        </table>
        <br />
        <br />
    </div>
    </ItemTemplate>
    <AlternatingItemTemplate>
    <div id="Kategori_alternating">
        <table border="0" cellspacing="0" cellpadding="8">
          <tr>
            <td class="kategoribilde" rowspan="3"><asp:Image Width="95px" runat="server" ID="imgVare" ImageUrl='<%# Eval ("hovedbilde","bilder/{0}") %>' /></td>
            <td colspan="2"><h4><asp:Label ID="lblTittel" runat="server" Text='<%# Eval("overskrift") %>'></asp:Label></h4></td>
          </tr>
          <tr>
            <td colspan="2"><asp:Label ID="lblBeskrivelse" runat="server" Text='<%# Eval ("varebeskrivelse") %>'></asp:Label>
            <asp:HyperLink ImageUrl="grafikk/knapp_lesmer.png" ID="lnkLesmer" runat="server" Text="Les mer" NavigateUrl='<%# Eval ("VareID","varedetalj.aspx?Vare={0}") %>'></asp:HyperLink><br /><br /></td>
          </tr>
          <tr>
            <td class="kategorilink">Kr <asp:Label ID="lblPris" runat="server" Text='<%# Eval("pris","{0:N2}") %>'></asp:Label></td>
            <td></td>
          </tr>
        </table>
        <br />
        <br />
    </div>
    
    </AlternatingItemTemplate>
        </asp:Repeater>
        
</asp:Content>

Open in new window

0
 
prairiedogCommented:
Now I see the problem. The HyperLink is defined within another server control, so it is not accessible direclty from your code-behind. The Repeater control has an event handler called ItemCreated, and you will need to find the HyperLink in the event handler first, then add those lines afterwards:
private void rprKategori_ItemCreated(Object sender, System.Web.UI.WebControls.RepeaterItemEventArgs)
{
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
     {
           HyperLink hl = (HyperLink)e.Item.FindControl("lnkLesmer")
           hl.Attributes.Add("OnMouseOver", "this.firstChild.src='" + Request.ApplicationPath() +  "/Images/knapp_lesmer_over.png'");

           hl.Attributes.Add("OnMouseOut", "this.firstChild.src='" + Request.ApplicationPath() + "/Images/knapp_lesmer.png'");
     }

}
0
 
RaggdogAuthor Commented:
I feel very stupid; but what do you mean by 'find the HyperLink in the event handler first' ?

This private void rprKategori_ItemCreated... I shall add that to the .cs file, right?
Am I supposed to write the ' OnItemCreated = rprKategori_ItemCreated' in the repeater control ass well?

Correct me, please...
0
 
prairiedogCommented:
this line will find the HyperLink in the event handler:
HyperLink hl = (HyperLink)e.Item.FindControl("lnkLesmer")

>>>This private void rprKategori_ItemCreated... I shall add that to the .cs file, right?
Right.

>>>Am I supposed to write the ' OnItemCreated = rprKategori_ItemCreated' in the repeater control ass well?
You got it right. :-)
0
 
RaggdogAuthor Commented:
Now I got the error: Compiler Error CS0118... I dont know what that means.

In my repeater I have an ItemTemplate, and an AlternatingItemTemplate. Both of them contains a hyperlink, and their ID=lnkLesmer. I know that an id is supposed to be unik, do you think that it's a problem that they have the same id? Just asking...
0
 
prairiedogCommented:
Sigh...I have tested my code before I posted and everything is fine.
1. Did you copy and paste my code into your ItemCreated event handler?
2. Do you use Visual Studio 2005?
0
 
prairiedogCommented:
>>>>Am I supposed to write the ' OnItemCreated = rprKategori_ItemCreated' in the repeater control ass well?
If you do this way, then remember to change the event handler's definition from "private" to "public". Otherwise, it will throw an error.
0
 
RaggdogAuthor Commented:
I really appreciate that you are helping me - I feel very lost

well yes, I did copy and past your code but I got an error from a missing identifier after "private void rprKategori_ItemCreated(Object sender, System.Web.UI.WebControls.RepeaterItemEventArgs)", so I added an 'e' at the end. Hope that was not a problem?

And yes I use Visual studio 2005.

This is my .cs

public partial class butikk : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    private void rprKategori_ItemCreated(Object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
     {
           HyperLink hl = (HyperLink)e.Item.FindControl("lnkLesmer");
           hl.Attributes.Add("OnMouseOver", "this.firstChild.src='" + Request.ApplicationPath() +  "/grafikk/knapp_lesmer_over.png'");

           hl.Attributes.Add("OnMouseOut", "this.firstChild.src='" + Request.ApplicationPath() + "/grafikk/knapp_lesmer.png'");
     }

    }




}

and my repeater looks like this:

<asp:Repeater ID="rprKategori" OnItemCreated="rprKategori_ItemCreated" runat="server" DataSourceID="SqlDataSource_rprKategoriVarer">
        <ItemTemplate>
        <div id="Kategori_item">
        <table border="0" cellspacing="0" cellpadding="8">
          <tr>
            <td class="kategoribilde" rowspan="3"><asp:Image Width="95px" runat="server" ID="imgVare" ImageUrl='<%# Eval ("hovedbilde","bilder/{0}") %>' /></td>
            <td colspan="2"><h4><asp:Label ID="lblTittel" runat="server" Text='<%# Eval("overskrift") %>'></asp:Label></h4></td>
          </tr>
          <tr>
            <td colspan="2"><asp:Label ID="lblBeskrivelse" runat="server" Text='<%# Eval ("varebeskrivelse") %>'></asp:Label>
            <asp:HyperLink ImageUrl="grafikk/knapp_lesmer.png" ID="lnkLesmer" runat="server" Text="Les mer" NavigateUrl='<%# Eval ("VareID","varedetalj.aspx?Vare={0}") %>'></asp:HyperLink><br /><br /></td>
          </tr>
          <tr>
            <td class="kategorilink">Kr <asp:Label ID="lblPris" runat="server" Text='<%# Eval("pris","{0:N2}") %>'></asp:Label></td>
            <td></td>
          </tr>
        </table>
        <br />
        <br />
    </div>
    </ItemTemplate>
    <AlternatingItemTemplate>
    <div id="Kategori_alternating">
        <table border="0" cellspacing="0" cellpadding="8">
          <tr>
            <td class="kategoribilde" rowspan="3"><asp:Image Width="95px" runat="server" ID="imgVare" ImageUrl='<%# Eval ("hovedbilde","bilder/{0}") %>' /></td>
            <td colspan="2"><h4><asp:Label ID="lblTittel" runat="server" Text='<%# Eval("overskrift") %>'></asp:Label></h4></td>
          </tr>
          <tr>
            <td colspan="2"><asp:Label ID="lblBeskrivelse" runat="server" Text='<%# Eval ("varebeskrivelse") %>'></asp:Label>
            <asp:HyperLink ImageUrl="grafikk/knapp_lesmer.png" ID="lnkLesmer" runat="server" Text="Les mer" NavigateUrl='<%# Eval ("VareID","varedetalj.aspx?Vare={0}") %>'></asp:HyperLink><br /><br /></td>
          </tr>
          <tr>
            <td class="kategorilink">Kr <asp:Label ID="lblPris" runat="server" Text='<%# Eval("pris","{0:N2}") %>'></asp:Label></td>
            <td></td>
          </tr>
        </table>
        <br />
        <br />
    </div>
    
    </AlternatingItemTemplate>
        </asp:Repeater>

Open in new window

0
 
RaggdogAuthor Commented:
I changed it to public, but it did not help.
0
 
RaggdogAuthor Commented:
You wrote:
>>>>If you do this way, then remember to change... about writing 'OnItemCreated = rprKategori_ItemCreated' directely in the code.

Is there another way?
I tried to doubleclick the repeater from designview, but then I got 'OnItemCommand="rprKategori_ItemCommand'
0
 
prairiedogCommented:
Let's do this from scratch:
1. Remove the event handler block from your cs file because we are going to let Visual Studio create the event handler for us.
2. Remove "OnItemCreated = rprKategori_ItemCreated" from your Repeater control.
3. Switch to Design view of your page. High-light your Repeater control. In the Properties window, you will see a lighting bolt icon, and click the icon to display all event handlers that Repeater supports. Double-click ItemCreated event handler, and an ItemCreated event handler will be created automatically in your .cs file.
4. Copy and past the code into the event handler.
5. Run your application. It must work. :-)
0
 
RaggdogAuthor Commented:
Now I did exactely as you said, and it did not work!
I did't get public void or private void - but protected void


CS0118: System.Web.HttpRequest.ApplicationPath er en egenskap, men brukes som en metode

This is my error message. It's in norwegian, but says something like ....ApplicationPath is a quality, but are used as a method.
0
 
prairiedogCommented:
Did you follow the steps I posted in my last comment?

>>>ApplicationPath is a quality, but are used as a method.
Sorry, forgot you are using C#. Remove "()" after ApplicationPath. Should look like: Request.ApplicationPath, without "()".
0
 
RaggdogAuthor Commented:
You are a genious, and you have an angels patience I must say!!
It finally works. Thank you so much!!
0
 
prairiedogCommented:
Hurray!!! Glad it is working now. I am heading off home now. Have a great weekend.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 14
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now