Solved

Image button with Javascript in a Datalist

Posted on 2009-03-30
6
1,129 Views
Last Modified: 2012-05-06
I have a image button in a datalist.  What I want ot be able to do is change the image on a onclick.  I can get it working when the image buttons is out side the datalist but can't firgure out how to append my code.  Please find the code I've got so far attached below
<script type="text/javascript">
 

function ChangeImage()

    {

        var imgbtn=document.getElementById("ImageButton2");

        imgbtn.src="Images/cart.jpg";

    }
 
 
 

</script>
 
 
 

 <asp:ImageButton ID="ImageButton2" runat="server" 

                    ImageUrl="~/Images/barrels.jpg" /><input type="button" onclick="javascript:ChangeImage()" value="Click" />

Open in new window

0
Comment
Question by:leap29
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 24017630
You can do it like this
<script type="text/javascript">

 

function ChangeImage(imgbtn)

    {

        imgbtn.src="Images/cart.jpg";

    }

 

 

 

</script>

 

 

 

 <asp:ImageButton ID="ImageButton2" runat="server" 

                    ImageUrl="~/Images/barrels.jpg" onclick="javascript:ChangeImage(this)" />

 

Open in new window

0
 

Author Comment

by:leap29
ID: 24017689
Thanks fro the reply, I've tried the code you suggested but get this error
Compiler Error Message: CS1026: ) expected
 

<asp:ImageButton ID="ImageButton1" runat="server" onclick="javascript:ChangeImage(this)" - line that the error is on.
 

full code for image button
 

asp:LinkButton ID="LinkButton2" runat="server" 

                                    CommandArgument='<%# Bind("jobid") %>' CommandName="DetailsViewRedirect" 

                                    Font-Size="Small" ForeColor="#9966FF" oncommand="LinkButton2_Command"  

                                    style="font-weight: 700; font-size: medium;" 

                                    Text='<%# Bind("jobtitle") %>' onclick="LinkButton2_Click"></asp:LinkButton>
 
 

script tag 
 

<script type="text/javascript">

   

       function ChangeImage(imgbtn)

    {

        imgbtn.src="Images/cart2.jpg";

    }

   

</script>

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 24017751
Sorry it should be OnClientClick and not onclick

<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="javascript:ChangeImage(this)" />
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:leap29
ID: 24017777
Thanks that seems to have worked although I have a small problem of when the page postback on the image button click the image changes back to it's orginally one.  Any suggestions on how to work around this?
0
 
LVL 16

Accepted Solution

by:
sunithnair earned 500 total points
ID: 24017855
You can do it like this
<script type="text/javascript">

    function ChangeImage(imgbtn)

    {

        imgbtn.src="Images/cart2.jpg";

        document.getElementById("<%=hdnImage.ClientID %>").value = imgbtn.id;

    }

    function SetImage()

    {

        var img = document.getElementById(document.getElementById("<%=hdnImage.ClientID %>").value);

        if(img != null)

            img.src = "Images/cart2.jpg";

    }

</script>

<body onload="SetImage();">

  <!-- YOUR CODE DATALIST AND ALL-->

   <asp:HiddenField ID="hdnImage" runat="server" />

</body>

Open in new window

0
 

Author Comment

by:leap29
ID: 24017918
Thanks got that working now, however I think I may have gone about my problem the wrong the way.
I'm going to open another question with regards to this.  hopefully you can help me with that.  Thanks a lot for the help with this question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript: Issue with onClick 5 36
Receiving a string from a WebService Push 21 37
SQL Exceptions 3 39
Google Sheets script efficiency 2 4
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now