Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
function Change() {


            document.getElementById('<%=img.ClientID %>').src = 'spinning-wheel.gif';

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
    <asp:Image AlternateText="aaa" ID="img" runat="server" ImageUrl="~/p.jpg" />
    <asp:Button ID="btn" runat="server" OnClientClick="Change()" Text="Click to Change"/>



    </div>
Posted
Comments
n.podbielski 28-Sep-12 9:19am    
And the question is?
Prathap Gangireddy 28-Sep-12 12:11pm    
On button click the javascript code is not working..which changes the image
Sergey Alexandrovich Kryukov 22-Jan-13 15:06pm    
One warning for you: some of your "answers" were not answers, so I removed it. And you also self-accepted a couple of them formally, which is usually considered as cheating (I would believe if you did not mean to).

You should add comment to any posts, reply to existing comments or use "Improve question". Please understand that such false answers get abuse reports, and certain sum of abuse report entail the ban of your account. A number of heavier abusers already lost their memberships; you don't want it. I just hope my warning can help you to avoid it.

Thank you for your understanding.
—SA

1) Don't ever give your html elements IDs that match tags. That in itself can be problematic.
ie. Change the id="img" to something else... anything else that doesn't match an html tag name.

2) Unless there is some reason for your code behind to know about this control, don't use the asp:Image tag. I would personally rather do it using a basic image tag.
ie. <img id="yourImageName" src="~/p.jpg" alt="aaa" />
This has the side benefit of making your javascript more explicit
ie. document.getElementById('yourImageName').src = 'spinning-wheel.gif';

3) Read Solution 1 for your other issue which is the postback.
 
Share this answer
 
v4
Comments
Prathap Gangireddy 28-Sep-12 13:55pm    
Marcus,

All the javascript code i write is not working in the button onClientClick event.

Below is the code.Its working fine when i call it from body onLoad event, but when tried to call it from the button click its not working.Is something wrong in the below code which creates a slideshow

<script type="text/javascript">

var list = new Array;
list[0] = "button6.gif";
list[1] = "button7.jpg";
list[2] = "p.jpg";



function Image(number) {

document.getElementById('<%=Wheel.ClientID %>').src = list[number];
if (number != list.length - 1) {
number += 1;
window.setTimeout("Image(" + number + ")", 1000);
}
else {
number = 0;
window.setTimeout("Image(" + number + ")", 1000);
}
return false;


}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Image ID="Wheel" runat="server" ImageUrl="~/button6.gif" />
<asp:Button Text="SlideShow" ID="btn" runat="server" onclick="btn_Click1"
/>


</div>

</div>
</form>
add

return false;

after

document.getElementById('<%=img.ClientID %>').src = 'spinning-wheel.gif';

and also try this when calling

OnClientClick="javascript:Change();"

becuase it cuases full postback if we don't use "return false" statement.


XML
function Change() {

document.getElementById('<%=img.ClientID %>').src = 'spinning-wheel.gif';

return false;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
    <asp:Image AlternateText="aaa" ID="img" runat="server" ImageUrl="~/p.jpg" />
    <asp:Button ID="btn" runat="server" OnClientClick="Change()" Text="Click to Change"/>



    </div>
 
Share this answer
 
v2
Implement this method this is working fine for me in all browser.


XML
<script  type="text/javascript">

        function Change() {
 var imagescount= document.getElementsByTagName("img");
 for (i = 0; i < imagescount.length; i++) {

                     if (imagescount[i].id.search("Wheel") != "-1") {
                         imagescount[i].src = spinning-wheel.gif;
                     }

                 }
            

        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
    <img id="Wheel" src="p.jpg" alt="aaa" />
    <asp:Button ID="btn" runat="server" OnClientClick="Change();return false;" Text="Click to Change"/>
       </div>

    </div>
 
Share this answer
 
v2

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