Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Oh, sorry for my bad post.
Here is the my problem:
I use a javascript code, for change color of button, when mouse go over the button.
I have buttons in all of my page, therefore I want create a masterpage.
Whit out masterpage and in a webpage, javascript code, work correctly.
But when I create masterpage and copy code in the masterpage, the color of the button doesn't change and javascript code doesn't work.

The code of my page(with out masterpage) that work correctly is:
<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true" CodeFile="with out master page.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <script language="JavaScript" type="text/JavaScript">
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
function obj1_onclick() {
    <script language="javascript" type="text/javascript" for="obj1" event="onclick">
// <!CDATA[
return obj1_onclick()
// ]]>
    <form id="form1" runat="server">
    <td bgcolor="#999966" style="text-align: center">
    with out master page<br />
&nbsp;<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/menu7.gif" onMouseOut="MM_swapImgRestore()"
                            onMouseOver="MM_swapImage('ImageButton3','','menu_7.gif',1)" AlternateText="onmouse"
                            ToolTip="onmouse" PostBackUrl="" meta:resourcekey="ImageButton3Resource1" />
Updated 5-Jul-11 9:24am
[no name] 5-Jul-11 10:20am    
If you want help you will need to provide more information.

What is the problem? Fully describe the issues including any error messages.
Post the relevant code snippets here, properly formatted. Don't include a link to an external download.

Edit your question to provide this information
#realJSOP 5-Jul-11 10:48am    
There's no way we can help you unless you show us what you're doing. And I deleted the link to the file from your message. If you want to show us code, show us code in your mesage.
Joan M 5-Jul-11 12:18pm    
Take a look at that... participating in a community like this one can help you a lot. The only problem here is that you should be able to post interesting and clear questions in order to make it possible to get answers... Possibly one of the members that have asked you to post more details could have helped you... you should definitely think on that and improve your question in order to allow people to help you... it is frustrating to try to help somebody and seeing that this person has not put some effort on making the question...

I don't know how this ever works, or what this code is doing, or why it's so complicated. However:


This should never work. Your button is NOT called ImageButton3 on the client. You need to use the ClientID property of the button in your code behind to get the actual client side ID, and inject that in to your JS. The way I'd do that is to use a variable for the name in your JS, then further up, create that variable and use a code block to read the value of ImageButton3.ClientID, inline.
Share this answer
nima_pw 10-Jul-11 11:00am    
Can you pls explain with code.
By my code, when mouse go on the button, the color of button be change.
Can you explain your way trough showing code that must be written in masterpage?
Christian Graus 11-Jul-11 17:34pm    
I am not giving you a copy and paste solution, because if I do, you won't understand the problem, which is trivial. Your button has a name that you can use to access it in code. That name is, apparently, ImageButton3. However, imagine you created a control, with a button called ImageButton3 on it, and used it twice in a page. ASP.NET obviously can't rely on the name you give, being unique on a page. So, it generates it's own ID for use in the page, which is the ID your code needs to use on the client side. The ClientID property, in C# or VB.NET, gives you the id it will use on the client. So, you need to write javascript in to your page, which stores this client side ID, and then use that ID to do your lookups. Something like

var imageButton3 = "<%ImageButton1.ClientID; %>"; should create a variable so you can do

onMouseOver="MM_swapImage(imageButton3); ( note, no quotes, I am using the variable ).
My problem solved.
In the following code:

<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/menu7.gif" onMouseOut="MM_swapImgRestore()"
onMouseOver="MM_swapImage('ImageButton3','','menu_7.gif',1)" AlternateText="onmouse"
ToolTip="onmouse" PostBackUrl="" meta:resourcekey="ImageButton3Resource1" /


In the MM_swapImage() function that is called for onMouseOver' event , instead 'ImageButton3' must be 'ctl00_ImageButton3':

<code><pre lang="vb">&lt;asp:ImageButton ID=&quot;ImageButton3&quot; runat=&quot;server&quot; ImageUrl=&quot;~/menu7.gif&quot; onMouseOut=&quot;MM_swapImgRestore()&quot;
onMouseOver=&quot;MM_swapImage(&#39;ctl00_ImageButton3&#39;,&#39;&#39;,&#39;menu_7.gif&#39;,1)&quot; AlternateText=&quot;onmouse&quot;
ToolTip=&quot;onmouse&quot; PostBackUrl=&quot;&quot; meta:resourcekey=&quot;ImageButton3Resource1&quot; /</pre>

Best regards,
Share this answer
Christian Graus 11-Jul-11 20:18pm    
OK, this works, for now. 'ctl00_ImageButton3' is the value in the ClientID property, now. But, that can change if you work on the page, which is why you should NEVER do this, and instead should do what I told you to do. Get the id from the ClientID property, don't hard code it.
nima_pw 12-Jul-11 2:29am    
Thanks Christian for your reply.
I put
var ImageButton3 = "<%ImageButton3.ClientID; %>";
In Load_Page and used this variable in MM_swapImage without quotes.
But didn't work.
I'm really confused :(
nima_pw 12-Jul-11 7:27am    
Why I can't use <%=ImageButton3.ClientID; %> as below?
<asp:ImageButton ID="ImageButton3" runat="server" onMouseOver="MM_swapImage('<%= ImageButton3.ClientID; %>','','menu_7.gif',1)" />
This code must send Client ID of ImageButton3 to MM_swapImage function, is not it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900