0 Replies - 4403 Views - Last Post: 06 February 2012 - 03:33 AM Rate Topic: ***** 1 Votes

#1 directiond  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 12

Retrieve(Display) the image in Gridview's ButtonField according to

Posted 06 February 2012 - 03:33 AM

Hi,

I use the ButtonField(Button Type= Image) in the Gridview for each rows...
Then i write the code to change the imageURL at runtime while user click on that button. And also store the status of that in dadtabase in STATUS column(field). In these i have to change the 4 images ...
it means default it displays the Black image and Status is "1"
on 1st click it displays Green and change STATUS to "2"
on 2nd Blue STATUS "3"
on 3rd Red STATUS "4"
on 4th again Black STATUS "1"...

My code is below..

Design...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
GridLines="None" onrowdatabound="GridView1_RowDataBound"
onrowcommand="GridView1_RowCommand"
onselectedindexchanged="GridView1_SelectedIndexChanged" >

<Columns>
<asp:ButtonField CommandName="pdf_click" ButtonType="Image"
ImageUrl="~/Image/Black.png" AccessibleHeaderText="ImgButton"
HeaderText="ID" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" HeaderStyle-CssClass = "hideGridColumn"
ItemStyle-CssClass="hideGridColumn"/>
<asp:BoundField DataField="Sub_task_Name" HeaderText="Sub_task_Name"
SortExpression="Sub_task_Name" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
</asp:GridView>



Code..is below

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="pdf_click")
{

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

int temp;
temp = Convert.ToInt32(GridView1.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 (GridView1.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 = GridView1.Rows[index];
((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Green.png";
Reset_Data();
}

else if (GridView1.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 = GridView1.Rows[index];
((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Blue.png";
}

else if (GridView1.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 = GridView1.Rows[index];
((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Red.png";
}

else if (GridView1.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 = GridView1.Rows[index];
((ImageButton)gvRow.Controls[0].Controls[0]).ImageUrl = "~/Image/Black.png";
}

_with1.ExecuteNonQuery();


}


}



Now i wants to retrieve or display the Images according to STATUSwise..
Suppose 1st record's STATUS is 2. and 2nd record's STATUS is 3 .. So whenever i run that(project/webpage) then Image of 1st record must be displayed as GreenImage.. and 2nd record'd image must be Blue Image..

So How can i do this?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1