改变图像(图像URL)这是在GridView的由onclick事件添加ButtonField字段的 [英] Change the Image(image url) of ButtonField which is added in Gridview by onclick event

查看:113
本文介绍了改变图像(图像URL)这是在GridView的由onclick事件添加ButtonField字段的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个GridView的专栏所以ButtonField字段,每个记录如下code添加ButtonField字段

I add a ButtonField in one Gridview's Column so there is Buttonfield for each Record as below code

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

在此我想通过点击该按钮字段来改变图像(图像URL)。

In this i wants to change the image(image url) by clicking on that Button field..

我添加一列STATUS在GridView ...

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

I wants to change the image of Buttonfield 4 times..

这意味着默认情况下它显示黑色形象,状态为1
1日点击它更改为蓝色图像和STATUS必须改为2
2日点击它更改为绿色形象STATUS3
 3日点击它更改为红色意象状态4。
并在4日再次单击它更改为黑图像STATUS1。

it means default it displays the "black image" and STATUS is "1" on 1st click it changes to "blue image" and STATUS must be changed to "2" on 2nd click it changes to "green image" STATUS "3" on 3rd click it changes to "red image" STATUS "4". and on 4th click again it changes to "black image" STATUS "1"..

它也正在像1单击它从黑色的形象从1变为蓝色和状态为2
但是当我再次点击,它不工作..再次更改它。它需要停止运行该项目。并重新运行它。然后它只有一个点击然后再需要停止该项目等等......
任何解决这个????

Its also working like on 1st click it change the image from black to blue and STATUS from "1" to "2" but when again i click on that it does not work.. to change it again. it needs to stop the project from running. and rerun it. then it works for only one click then again needs to stop the project and so on... Any Solution for this????

我的code是如下。

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

protected void GvDetail_RowCommand(object sender, GridViewCommandEventArgs e) {

如果(e.CommandName ==GGG)
        {

if (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();


    }


}

我该怎么办呢?

在此先感谢

推荐答案

您应该添加命令:

绑定数据

写code的命令

http://i.stack.imgur.com/c1j0Q.png

http://i.stack.imgur.com/c1j0Q.png

preSS

P.S。你也可以使用的FindControl

P.S. you could have used findcontrol

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

but since your gridview is not within a template -

所以它不会有一个ID

so it doesn't have an Id

这篇关于改变图像(图像URL)这是在GridView的由onclick事件添加ButtonField字段的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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