求助!datalist嵌套datalist的问题
sql数据库table1中有riqi和zhuti两个字段,我想在datalist中显示如下效果:
日期1
主题1
主题2
主题3
日期2
主题4
主题5
。。。。
也就是如果多个主题如果在同一天发布,就归为同一天的下面显示,我在网上找到一段datalist嵌套的代码,修改如下:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Dim sqlstr As String
Dim lstr As String
Dim con As New SqlConnection("......")
sqlstr="SELECT distinct Convert(VARCHAR(10),riqi,120) AS RIQI2 FROM table1"
Dim da3 As New SqlDataAdapter(sqlstr,con)
Dim ds3 As New System.Data.DataSet
da3.Fill(ds3, "table1")
DataList2.DataSource = ds3.Tables("table1").DefaultView
DataList2.DataBind()
Dim i As Integer
For i = 0 To DataList2.Items.Count - 1
Dim Riqi_date As System.Web.UI.WebControls.Label = DataList2.Items(i).FindControl("RIQI2")
Dim dlsC As System.Web.UI.WebControls.DataList = DataList2.Items(i).FindControl("DataList1")
lstr="SELECT zhuti,riqi,Convert(VARCHAR(10),riqi,120) AS RIQI1 FROM table1 WHERE RIQI1 = "& Riqi_date.text &" ORDER BY riqi DESC"
Dim da2 As New SqlDataAdapter(lstr,con)
Dim ds2 As New System.Data.DataSet
da2.Fill(ds2, "table1")
dlsC.DataSource = ds2.Tables("table1").DefaultView
dlsC.DataBind()
Next
End If
End Sub
</script>
<form runat="server">
<asp:DataList id="DataList2" runat="server">
<ItemTemplate>
<div style="background-color:#edede3"><%# Eval("RIQI2")%><br></div>
<asp:DataList id="DataList1" runat="server">
<Itemtemplate>
<%# Eval("zhuti")%> <%# Eval("riqi")%>
</Itemtemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</form>
一行代码出现错误:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
lstr="SELECT zhuti,riqi,Convert(VARCHAR(10),riqi,120) AS RIQI1 FROM table1 WHERE RIQI1 = "& Riqi_date.text &" ORDER BY riqi DESC"
请大家帮忙看看,该怎样修改呢?
[解决办法]
嵌套的越多应该效率越低,服务器要解析这些控件的
你的ItemTemplate里面没有RIQI2这个控件