嵌套datalist对应分类列表
前台:
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound"> <ItemTemplate> <table width="500px"> <tr> <td style="background-color: #194477; color: White; height: 35px;"> <%# Eval("cateName") %> </td> </tr> <tr> <td> <asp:DataList ID="DataList2" runat="server"> <ItemTemplate> <%# Eval("btitle") %> </ItemTemplate> </asp:DataList> </td> </tr> </table> </ItemTemplate> </asp:DataList>
后台:
DataAccess d = new DataAccess(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } public void bind() { DataList1.DataSource = d.getDataSet("select cateName from tb_cate where bigCate='人文关怀'", false); DataList1.DataBind(); } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { DataList d2 = (DataList)e.Item.FindControl("DataList2"); DataRowView drv = (DataRowView)e.Item.DataItem; object cate = drv["cateName"]; if (cate != null) { d2.DataSource = d.getDataSet("select distinct btitle from tb_video where cateName='" + cate.ToString() + "'", false); d2.DataBind(); } }
显示效果