datalist中根据条件判断颜色交替
我在datalist中以日期/主题的形式显示列表,其中如果日期相同则隐藏日期,最后的形式是
日期1
主题1(白)
主题2(灰)
主题3(白)
日期2
主题4(白)
主题5(灰)
...
我用了以下一段代码:
Private LastDate As DateTime = DateTime.Now.AddYears(-100)
Protected Sub DataList1_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim riqi As DateTime = DirectCast(DataBinder.Eval(e.Item.DataItem, "riqi"), DateTime)
If LastDate = riqi Then
Dim Panel1 As Panel = TryCast(e.Item.FindControl("Panel1"), Panel)
Panel1.Visible = False
End If
LastDate = riqi
End If
End Sub
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
RepeatLayout="Flow" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<%# Eval("riqi")%><br>
</asp:Panel>
<%# Eval("zhuti")%><br>
<br>
</ItemTemplate>
</asp:DataList>
可以达到我希望的效果,现在的问题是,怎样设置颜色交替?即从每个日期开始交替颜色,而不是从主题1开始交替颜色,否则主题4的那一行就成了灰色。请各位朋友帮忙看看!谢谢!
[解决办法]
你可以这样
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Dim ds As New System.Data.DataSet()
Dim dataTable1 As New System.Data.DataTable("BlogUser")
Dim dr As System.Data.DataRow
dataTable1.Columns.Add(New System.Data.DataColumn("UserId", GetType(System.Int32)))
dataTable1.Columns.Add(New System.Data.DataColumn("UserName", GetType(System.String)))
dataTable1.Columns.Add(New System.Data.DataColumn("riqi", GetType(System.DateTime)))
dr = dataTable1.Rows.Add(New [Object]() {1, "【孟子E章】" + 1.ToString(), DateTime.ParseExact("2011-12-12", "yyyy-MM-dd", Nothing)})
dr = dataTable1.Rows.Add(New [Object]() {2, "【孟子E章】" + 2.ToString(), DateTime.ParseExact("2011-12-12", "yyyy-MM-dd", Nothing)})
dr = dataTable1.Rows.Add(New [Object]() {3, "【孟子E章】" + 3.ToString(), DateTime.ParseExact("2012-12-11", "yyyy-MM-dd", Nothing)})
dr = dataTable1.Rows.Add(New [Object]() {4, "【孟子E章】" + 4.ToString(), DateTime.ParseExact("2012-12-11", "yyyy-MM-dd", Nothing)})
dr = dataTable1.Rows.Add(New [Object]() {5, "【孟子E章】" + 5.ToString(), DateTime.ParseExact("2012-12-11", "yyyy-MM-dd", Nothing)})
DataList1.DataSource = dataTable1
DataList1.DataBind()
End If
End Sub
Private LastDate As DateTime = DateTime.Now.AddYears(-100)
Protected Sub DataList1_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim riqi As DateTime = DirectCast(DataBinder.Eval(e.Item.DataItem, "riqi"), DateTime)
If LastDate = riqi Then
Dim Panel1 As Panel = TryCast(e.Item.FindControl("Panel1"), Panel)
Panel1.Visible = False
End If
Dim Panel2 As Panel = TryCast(e.Item.FindControl("Panel2"), Panel)
If e.Item.ItemIndex Mod 2 = 0 Then
Panel2.ForeColor = Drawing.ColorTranslator.FromHtml("#FFFFFF")
Else
Panel2.ForeColor = Drawing.ColorTranslator.FromHtml("#EEEEEE")
End If
LastDate = riqi
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body style="background:pink">
<form id="form1" runat="server">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
RepeatLayout="Flow" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<%# Eval("riqi")%>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server"><%# Eval("UserName")%></asp:Panel>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html