Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a html image in my asp.net page. Intially it is set to self.png.

Image will be set dynamically from server side means from code behind. How is it possible.

<img alt="" src="self.png" runat="server" class ="igte_rPayNet_ButtonImg" />

Any one please provide useful info.

Thanks

What I have tried:

on page_load event how can i change the image.
i was tried by using java script it's not working.

var img1 = document.getElementByClassNames("igte_rPayNet_ButtonImg");
var imag1= document.getElementsByTagName("igte_rPayNet_ButtonImg");

alert(img1);
alert(imag1);

trying the below i am getting objectHtmlcollections errors

var ig = document.getElementById("igte_rPayNet_ButtonImg").src = "../Images/joy.png";
alert(ig);


using JS am unable to getting it, and unable changing the image.
Posted

Give the img tag an ID and then you should be able to reach it from the server-side:
HTML
<img alt="" src="self.png"  runat="server" class="igte_rPayNet_ButtonImg" id="yourIdHere" />

C#
yourIdHere.Src = "path-here.png"
 
Share this answer
 
v2
Comments
Kapuraveni BharathKumar 11-Feb-16 6:56am    
Thanks for the reply,
with out using img tag id, i want to do this.
using img tag class name if it is possible? to change the image by dynamically or using javascript.
Thomas Daniels 11-Feb-16 7:15am    
I'm not sure why you want to do this, but anyway, it is possible. You can either search for the control, or use the PreRender event: http://stackoverflow.com/questions/2766408/how-to-select-an-element-by-class-instead-of-id-in-asp-net
The method is called getElementsByClassName, and it returns an array;

var img1 = document.getElementsByClassName("igte_rPayNet_ButtonImg");

if (img1.length > 0)
{
    img1[0].src = "joy.png";
}
 
Share this answer
 
Here is the Answer. thank you all for your support.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$('.igte_rPayNet_ButtonImg').attr("src", '../Images/joy.png');
});
</script>
 
Share this answer
 

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