Reputation: 877
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>
In this i wants to change the image(image url) by clicking on that Button field..
I add a column STATUS in the Gridview...
I wants to change the image of Buttonfield 4 times..
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"..
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????
My code is as below..
protected void GvDetail_RowCommand(object sender, GridViewCommandEventArgs e) {
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();
}
}
How can i do it?
Thanks in advance
Upvotes: 1
Views: 9107
Reputation: 148624
you should add command :
bind data
write code for command
https://i.sstatic.net/c1j0Q.png
press
P.S. you could have used findcontrol
but since your gridview is not within a template -
so it doesn't have an Id
Upvotes: 1
Reputation: 1036
You need to attach a handler to the RowCommand
event. In it you can find which button was clicked from the GridViewCommandEventArgs
(CommandSource I think) and the change the url to the image.
Upvotes: 1