更改图像(图像URL),其在的GridView通过onclick事件

更改图像(图像URL),其在的GridView通过onclick事件

问题描述:

加入ButtonField字段的我添加ButtonField字段在一个GridView的列,以便有ButtonField字段对于每个记录,如下代码更改图像(图像URL),其在的GridView通过onclick事件

<asp:GridView ID="GvDetail" runat="server"> 
    <Columns> 
     <asp:ButtonField ButtonType="Image" ImageUrl="~/Img/Black.png" CommandName="ggg" /> 
    </Columns> 
</asp:GridView> 

在这方面,我想通过点击该按钮场改变图像(图像URL)..

我添加一列STATUS在GridView ...

我想改变的ButtonField字段4倍的图像..

它意味着默认显示“黑色图像”,状态为“1” 第一次点击它变成“蓝色图像”,状态必须更改为“2” 第二次点击它变成“绿色图像”状态“3” 第三次点击它变成“红色图像”状态“4”。 和4日再次单击它更改为“黑图像”状态“1” ..

它也像工作在1日点击它从黑色图像变为蓝色和状态从“1”到“2” 但当我再次点击它不工作..再次改变它。它需要停止运行项目。并重新运行它。那么它只适用于一次点击,然后再次需要停止项目等...... 任何解决方案?

我的代码是如下..

保护无效GvDetail_RowCommand(对象发件人,GridViewCommandEventArgs E) {

如果(e.CommandName == “GGG”) {

 int index = Convert.ToInt32(e.CommandArgument); 

     int temp; 
     temp = Convert.ToInt32(GvDetail.Rows[index].Cells[1].Text); 
     // gets the ID of clicked row from First Column(Name="Id") 

     com = new SqlCommand(); 
     var _with1 = com; 
     _with1.Connection = con; 


     if (GvDetail.Rows[index].Cells[3].Text == "1") 
     { 
      string str1; 
      str1 = "update SubTask set Status='2' where ID= '" + temp + "'"; 
      _with1.CommandText = str1; 
      Label4.Text = "Start"; 
      Label4.Text = str1; 

      GridViewRow gvRow = GvDetail.Rows[index]; 
      ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Green.png"; 
     } 

     else if (GvDetail.Rows[index].Cells[3].Text == "2") 
     { 
      string str1; 
      str1 = "update SubTask set Status='3' where ID= '" + temp + "'"; 
      _with1.CommandText = str1; 
      Label4.Text = "Start"; 
      Label4.Text = str1; 
      GridViewRow gvRow = GvDetail.Rows[index]; 
      ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Blue.png"; 
     } 

     else if (GvDetail.Rows[index].Cells[3].Text == "3") 
     { 
      string str1; 
      str1 = "update SubTask set Status='4' where ID= '" + temp + "'"; 
      _with1.CommandText = str1; 
      Label4.Text = "Start"; 
      Label4.Text = str1; 
      GridViewRow gvRow = GvDetail.Rows[index]; 
      ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Red.png"; 
     } 

     else if (GvDetail.Rows[index].Cells[3].Text == "4") 
     { 
      string str1; 
      str1 = "update SubTask set Status='1' where ID= '" + temp + "'"; 
      _with1.CommandText = str1; 
      Label4.Text = "Start"; 
      Label4.Text = str1; 
      GridViewRow gvRow = GvDetail.Rows[index]; 
      ((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Black.png"; 
     } 

     _with1.ExecuteNonQuery(); 


    } 


} 

我该怎么办?

在此先感谢

+0

看到我的检查答案 – 2012-02-04 13:06:32

你应该添加命令:

enter image description here

绑定数据

enter image description here

写代码指令

http://i.stack.imgur.com/c1j0Q.png enter image description here

enter image description here

enter image description here

附:你也可以使用的FindControl

但因为你的GridView是不是一个模板内 -

所以它不会有一个ID

enter image description here

+0

嘿谢谢.. 你代码工作.. 但我有一个问题再次阅读我的问题因为我编辑.. – Dip 2012-02-06 05:39:02

+1

@stranger - 如果罗伊已经帮助 - 然后我会标记为答案,并提出一个单独的问题。 Royi很明显地把工作放在这里,我认为值得 - 这个新问题也会得到更多的兴趣和贡献者。当然只是MHO – 2012-02-06 10:29:11

你需要一个处理程序连接到RowCommand事件。在这里你可以找到从GridViewCommandEventArgs(我认为是CommandSource)点击了哪个按钮,并将url更改为图像。