切换图片Asp.Net [英] Toggle Image Asp.Net

查看:78
本文介绍了切换图片Asp.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 onclick 事件更改图像按钮的图像?
Gridview 中,图像"按钮用于所有记录..
默认图像是黑色图像..
第一次单击时,必须将其更改为蓝色图像"
在第二个点击绿色图像
在第3次点击红色图片
在第4次单击时再次转换为黑色图像...
如何在 Asp.net vb ??
中执行此操作

我的代码如下..

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..

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



但这给了我一个错误..

指定的参数超出有效值范围.参数名称:index

在此代码行中>昏暗的imgbutton作为ImageButton = DirectCast(GridView1.Rows(i).Cells(2).Controls(0),ImageButton)




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??

推荐答案




这可能不是解决方案.但是我不明白为什么您要遍历整个网格并获取所有图像按钮的引用?如果我错了,请纠正我.

在图像按钮上单击,获取参考并更改该按钮的URL.
就像,这在C#中.我不确定在VB中如何.

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.

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



希望对您有所帮助.



Hope this helps.


在onclick事件中,您需要将图像src或图像按钮的源更改为新的图像URL ...
in the onclick event you need to change the image src or source of your image button to your new image url...


感谢每个给我解决方案的人...:)
Thanks to Everyone who gives me a solutions... :)


这篇关于切换图片Asp.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆