Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. asp.net toggle image

asp.net toggle image

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nettutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    disha desani
    wrote on last edited by
    #1

    How to change the image of the Imagebutton by clicking on it. I wanna chanege it 4 times. Means 4 images available. default is Black image on 1st click it changed to Blue image on 2nd click Green Image on 3rd click Red image and on 4th click again black image.... Button is used in Gridview..

    R L 2 Replies Last reply
    0
    • D disha desani

      How to change the image of the Imagebutton by clicking on it. I wanna chanege it 4 times. Means 4 images available. default is Black image on 1st click it changed to Blue image on 2nd click Green Image on 3rd click Red image and on 4th click again black image.... Button is used in Gridview..

      R Offline
      R Offline
      Rajeev Jayaram
      wrote on last edited by
      #2

      Usually at this situation, I keep hidden imagebuttons and make it visible/hide accordingly. Note: I am not sure if this is the right way to do this.

      D 1 Reply Last reply
      0
      • D disha desani

        How to change the image of the Imagebutton by clicking on it. I wanna chanege it 4 times. Means 4 images available. default is Black image on 1st click it changed to Blue image on 2nd click Green Image on 3rd click Red image and on 4th click again black image.... Button is used in Gridview..

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You Can check on each image button click event. Take a look at the algorithm below if ( button.ImageURL = [Image1URL]) then button.ImageURL = [Image2URL] Else if (button.ImageURL == [Image2URL] ) THEN button.ImageURL = [Image3URL] ELSE IF (button.ImageURL == [Image3URL] THEN button.ImageURL = [Image4URL] ELSE button.ImageURL = [Image1URL] END IF Hope this algorithm help you out..

        D 2 Replies Last reply
        0
        • L Lost User

          You Can check on each image button click event. Take a look at the algorithm below if ( button.ImageURL = [Image1URL]) then button.ImageURL = [Image2URL] Else if (button.ImageURL == [Image2URL] ) THEN button.ImageURL = [Image3URL] ELSE IF (button.ImageURL == [Image3URL] THEN button.ImageURL = [Image4URL] ELSE button.ImageURL = [Image1URL] END IF Hope this algorithm help you out..

          D Offline
          D Offline
          disha desani
          wrote on last edited by
          #4

          I tried as u said and My code is as below.. 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" 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??

          S 1 Reply Last reply
          0
          • R Rajeev Jayaram

            Usually at this situation, I keep hidden imagebuttons and make it visible/hide accordingly. Note: I am not sure if this is the right way to do this.

            D Offline
            D Offline
            disha desani
            wrote on last edited by
            #5

            :) yes but it is possible only if we have only two images to toggle. But here i have 4 images.. :(

            1 Reply Last reply
            0
            • D disha desani

              I tried as u said and My code is as below.. 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" 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??

              S Offline
              S Offline
              Satish_S
              wrote on last edited by
              #6

              Hi

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

              the above error will occur if GridView1.Rows(i).Cells(2).Controls(0)is exceeding its range. check for the columns you have in grid, if suppose if you have only one row with 1 cell then grid you will receive the error as you mentioned. Try to use Immediate window when you are debugging and check by copying "GridView1.Rows(1).Cells(2).Controls(0)" , if it throws error reduce the row and cell index numbers. Hope this will help. thanks

              D 1 Reply Last reply
              0
              • L Lost User

                You Can check on each image button click event. Take a look at the algorithm below if ( button.ImageURL = [Image1URL]) then button.ImageURL = [Image2URL] Else if (button.ImageURL == [Image2URL] ) THEN button.ImageURL = [Image3URL] ELSE IF (button.ImageURL == [Image3URL] THEN button.ImageURL = [Image4URL] ELSE button.ImageURL = [Image1URL] END IF Hope this algorithm help you out..

                D Offline
                D Offline
                disha desani
                wrote on last edited by
                #7

                Hey thanks i tried your algo. First it gives me an error. but i solved it and its working well :)

                S 1 Reply Last reply
                0
                • S Satish_S

                  Hi

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

                  the above error will occur if GridView1.Rows(i).Cells(2).Controls(0)is exceeding its range. check for the columns you have in grid, if suppose if you have only one row with 1 cell then grid you will receive the error as you mentioned. Try to use Immediate window when you are debugging and check by copying "GridView1.Rows(1).Cells(2).Controls(0)" , if it throws error reduce the row and cell index numbers. Hope this will help. thanks

                  D Offline
                  D Offline
                  disha desani
                  wrote on last edited by
                  #8

                  Thanks for Solution...

                  1 Reply Last reply
                  0
                  • D disha desani

                    Hey thanks i tried your algo. First it gives me an error. but i solved it and its working well :)

                    S Offline
                    S Offline
                    Satish_S
                    wrote on last edited by
                    #9

                    You are Welcome.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups