datalist循环判断显示隐藏
大家好!我是新手,请教一个问题:
datalist中有一个循环输出的项是日期,还有一个项是主题,输出后得到
日期1
主题1
日期2
主题2
日期3
主题3
我想得到的结果是,如果日期1和日期2是同一天,则只显示日期1,日期2隐藏,效果如下:
日期1
主题1
主题2
日期3
主题3
我想这里有一个循环判断的问题,应该怎样写VB语句呢,谢谢大家!
[解决办法]
下面是一个例子。希望你能看明白
<%@ 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-12", "yyyy-MM-dd", Nothing)})
dr = dataTable1.Rows.Add(New [Object]() {5, "【孟子E章】" + 5.ToString(), DateTime.ParseExact("2012-12-12", "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
LastDate = riqi
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<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")%><br>
</asp:Panel>
<%# Eval("UserName")%><br>
<br>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>