Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 23rd, 2013, 07:58 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default MouseOver by adding an attribute

Hello Everyone, i am trying to do a mouseOver event in my Default.aspx page where the image changes on a mouseOver. The code I have below trying to add an attribute which changes the imageButton picture does not work, when the page is requested it gets rendered fine but when i enter the imageButton control with my cursor the browser comes up "Web Page Error". When i dubug the debugger comes up with this "breaking on JScript run time error - Object expected".

Any help would be greatly appreciated.
Mark

Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ImageButton ID="ImageButton1" runat="server" Height="139px" 
            ImageUrl="~/Images/Picture2.jpg" Width="158px" />
        </div>
    </form>
</body>
</html>
Page Behind
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    ImageButton1.Attributes["onMouseOver"] = "javascript:swapImageIn('~/Images/Picture1.jpg');return true;";
    ImageButton1.Attributes["onMouseOut"] = "javascript:swapImageOut();return true;";
    }
}
 
Old May 23rd, 2013, 05:45 PM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

I have also tried the below but it still won't work

Mark

Code:
ImageButton1.Attributes.Add("onMouseOver", "javascript:swapImageIn('~/Images/Picture2.jpg');return true;");
 
Old May 24th, 2013, 03:12 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You haven't defined the swapImage JavaScript function anywhere. Does your page include it somehow or does it have a reference to an external JavaScript file you didn't post?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 24th, 2013, 08:49 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Hi Imar and thanks for your reply. No I have not defined the swapImage javascript nor is there an external reference.

Would it be something like

Quote:
<script type ="text/javascript" id = "swapImageIn">
{
this.src = "~/Images/Picture1.jpg"
}
</script>
Thanks Again
Mark

Last edited by markhh; May 24th, 2013 at 09:13 AM.. Reason: Updating
 
Old May 24th, 2013, 10:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Nope, you need to define a real function using something like.

function swapImageIn()
{
// .... Code here
}

Did you copy this code from somewhere? If so, didn't they include the swapImageIn and Out functions?

You may want to Google a bit for this as there are lots of useful tutorials out there. For example:

http://www.htmlite.com/JS019.php

http://stackoverflow.com/questions/1...image-on-hover

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
markhh (May 27th, 2013)
 
Old May 27th, 2013, 04:34 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

Hi Imar, yes I will check the suggested sites out,

Thanks again
Mark





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select nodes whose attribute matches another node's attribute? nikolai XSLT 5 April 18th, 2018 11:30 AM
MouseOver/Out Filespit ASP.NET 3.5 Basics 3 February 26th, 2011 09:24 AM
Mouseover in xslt rabs XSLT 3 March 16th, 2009 05:48 AM
oPacity Mouseover crmpicco Javascript How-To 3 July 1st, 2005 09:43 AM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.