在gridview控件中使用IIf

时间:2012-01-04 13:35:07

标签: asp.net vb.net

我正在尝试根据gridview里面的条件更改图像控件的图像url,这是我的标记:

<asp:TemplateField HeaderText="Status">
    <ItemTemplate>
        <asp:Image ID="Image1" runat="server"
            ImageUrl='<%# IIf(CDate(Eval("startdate")) < CDate(Eval("expdate")),"~/iWebmallManager/Images/bullet_red.png",
                "~/iWebmallManager/Images/bullet_green.png" %>'
        /> 
    </ItemTemplate>                                                  
    <HeaderStyle HorizontalAlign="Left" />
    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>

IIf声明无效。我不知道我在做什么,请帮助

1 个答案:

答案 0 :(得分:0)

你应该在codebehind中这样做,因为它增加了可读性并且更容易调试:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim rowView = DirectCast(e.Row.DataItem,DataRowView)
        Dim Image1 = DirectCast(e.Row.FindControl("Image1"),Image)
        Dim startdate = DirectCast(rowView("startdate"), Date)
        Dim expdate = DirectCast(rowView("expdate"), Date)
        If startDate < expdate
            Image1.ImageUrl = "~/iWebmallManager/Images/bullet_red.png"
        Else
            Image1.ImageUrl = "~/iWebmallManager/Images/bullet_green.png"
        End If
    End If
End Sub