我在asp.net和vb.net中有一个数据网格,我想用两个可能的图标显示某一行项目的状态。
最简单的方法是什么?
我有一个检查验证的函数,并返回一个使用datagrid的某些字段的布尔值。
(你可以用c#回答)
答案 0 :(得分:1)
您需要决定在您的网页代码中加载哪个图片。
protected void Page_Init(object sender, EventArgs e)
{
// first you have to hook up the event
datagrid.ItemDataBound += datagrid_ItemDataBound;
}
// once the grid is being bound, you have to set the status image you want to use
private void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {
Image img = (Image)e.Item.FindControl("ImageControlName");
if( ValidationFunction() ) {
img.ImageUrl = "first_status_image.jpg";
}
else
{
img.ImageUrl = "second_status_image.jpg";
}
}
}
答案 1 :(得分:1)
我应该说最好的办法是使用TemplateColumn和一些代码:
<asp:DataGrid runat="server" ID="DataGrid1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image runat="server" ID="RowImage" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
Dim imageControl As Image
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
imageControl = DirectCast(e.Item.FindControl("RowImage"), Image)
If MyValidationFunction() Then
imageControl.ImageUrl = "icon1.gif"
Else
imageControl.ImageUrl = "icon2.gif"
End If
End If
End Sub