Reputation: 4630
I have a gridview which has a checkbox column and an image column
now if the checkbox is selected the image column should show a green tick image and if checkbox is not checked it should show a wrong image icon in consecutive rows.
the .aspx page has
<asp:TemplateField HeaderText="Backup Session Status"
SortExpression="backupsessionstatus">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("backupsessionstatus") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("backupsessionstatus") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Backup Session Status">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
and the .cs file is:
foreach (GridViewRow myRow in GridView1.Rows)
{
Image img1 = (Image)myRow.FindControl("Image1");
CheckBox chkbox1 = (CheckBox)myRow.FindControl("CheckBox1");
if (chkbox1.Checked)
{
img1.ImageUrl = "greenimage.jpg";
}
else
{
img1.ImageUrl = "redimage.jpg";
}
}
it is not displaying no image in the column
please suggest a way... thanks
Upvotes: 3
Views: 10332
Reputation: 4630
Thanks for the inputs but i got the answer.. this is how
<asp:TemplateField HeaderText="Backup Session Status"
SortExpression="backupsessionstatus">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("backupsessionstatus") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("backupsessionstatus") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Backup Session Status">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/NewFolder1/1258341827_tick.png"/>
</ItemTemplate>
</asp:TemplateField>
This is the .CS code
foreach (GridViewRow myRow in GridView1.Rows)
{
Image img1 = (Image)myRow.FindControl("Image1");
CheckBox chkbox1 = (CheckBox)myRow.FindControl("CheckBox1");
if (chkbox1.Checked)
{
img1.ImageUrl = "~/greenimage.jpg";
}
else
{
img1.ImageUrl = "~/redimage.jpg";
}
}
I learnt that you have to give a imageurl in the aspx page and then modify it to any image in the code behind. if you dont give the image url in the aspx page it wont work...
Upvotes: 4
Reputation: 8421
Try switching the image url in the OnRowDataBound
event of the GridView
.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");
if (chk != null)
{
Image img1 = (Image)e.Row.FindControl("Image1");
if (chk.Checked)
img1.ImageUrl = "greenimage.jpg";
else
img1.ImageUrl = "redimage.jpg";
}
}
Upvotes: 1
Reputation: 144112
Have you stepped through the code in debug and ensured your foreach
routine is running as you expect it to? Always check the obvious first... are your image paths correct? no "/" will mean it is looking for the image relative to the folder the page is loaded in.
Upvotes: 1