我在解決方案瀏覽器的兩個圖像在該位置處顯示圖像 -DataGridView的細胞返回NULL,而試圖在數據網格細胞
〜/圖像/ Active.jpg & 〜/圖像/ Inactive.jpg
我想顯示這些圖像在我的網格視圖單元取決於單元格值,即「活動」或「無效」。
我至今嘗試下面給出:
GridView的代碼在頁面源文件:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="GridView1_DataBound1" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" >
<Columns>
<asp:BoundField DataField="ServerName" HeaderText="ServerName" SortExpression="ServerName" />
<asp:BoundField DataField="Application" HeaderText="Application" SortExpression="Application" />
<asp:BoundField DataField="Instance" HeaderText="Instance" SortExpression="Instance" />
<asp:BoundField DataField="Environment" HeaderText="Environment" SortExpression="Environment" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imgID" imageurl="" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
代碼隱藏文件.aspx.cs:
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[4].Text == "Active")
{
Image img = (Image)row.FindControl("imgID");
img.ImageUrl= "";
}
else
{
Image img = (Image)row.FindControl("imgID");
img.ImageUrl = "~/Images/Inactive.jpeg";
}
}
請注意,我不能使用rowdatabound。我已經寫了上面的代碼中的一個函數,我正在檢查一些東西,並將單元格值更新爲Active或Inactive。
出現的問題是DataGrid的Cell返回圖像的空值。
請建議解決方案來解決這個問題。
爲什麼你分配空 - 'img.ImageUrl =「」;'爲活動?它應該是'「〜/ Images/Inactive.jpeg」;'? –
爲什麼不能使用行數據綁定事件? – Saurabh
@KarthickRaju,那真是空虛。但即使我把網址,它不工作。 –