Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
How to Change Image of Image Button by onclick event??
In a Gridview Image button is used for all records..
Default image is Black color image..
on 1st click it must be change to Blue Image
on 2nd click Green Image
on 3rd click Red Image
on 4th click again converts to Black color Image...
How can i do this in Asp.net vb??


My code is as below..

VB
Protected Sub ImageButton2_Click1(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
     
       For i As Integer = 0 To GridView1.Rows.Count - 1
          

           Dim imgbutton As ImageButton = DirectCast(GridView1.Rows(i).Cells(2).Controls(0), ImageButton)

           If imgbutton.ImageUrl = "~/Image/Red.png" Then
               imgbutton.ImageUrl = "~/Image/Green.png"
               ' Next
           ElseIf imgbutton.ImageUrl = "~/Image/Green.png" Then
               imgbutton.ImageUrl = "~/Image/Blue.png"
           ElseIf imgbutton.ImageUrl = "~/Image/Blue.png" Then
               imgbutton.ImageUrl = "~/Image/Black.png"
           Else
               imgbutton.ImageUrl = "~/Image/Red.png"
           End If

       Next

   End Sub


but it gives me an error like..

Specified argument was out of the range of valid values. Parameter name: index

in this code line--> Dim imgbutton As ImageButton = DirectCast(GridView1.Rows(i).Cells(2).Controls(0), ImageButton)


Any solution for that??
Posted
Updated 1-Feb-12 18:16pm
v2
Comments
Herman<T>.Instance 31-Jan-12 4:10am    
If you know the url of the image you should be able to determine the next ImageUrl for your imagebutton.

Hi,


This might not be the solution.But i dint understand why are you lopping through the whole grid and getting reference of all image buttons?Correct me if i am wrong.

On the image button click, get the reference and change the url of that button.
Like,this in C#.Iam not sure how it is in VB.

C#
ImageButton ibtn1 = sender as ImageButton;
//then all your if conditions.


Hope this helps.
 
Share this answer
 
Comments
disha desani 2-Feb-12 1:37am    
Hey Thanks.. :) its working.... ;)
manognya kota 2-Feb-12 1:45am    
Welcome:) Good luck!Happy coding
in the onclick event you need to change the image src or source of your image button to your new image url...
 
Share this answer
 
Thanks to Everyone who gives me a solutions... :)
 
Share this answer
 
Comments
manognya kota 2-Feb-12 1:44am    
Welcome:) Suggestion: do not post this as solution:) instead post as comment for your question.Good luck!

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