(原创)第一次使用Datalist的嵌套功能,不明白它的原理还真是有点晕
经过了调试之后终于知道了,其实它的使用原理跟C语言中的for循环类似我使用的是两个Datalist的嵌套就像C语言中的两个for循环嵌套的使用
for(int i = 0 ; i < 5; i++)
{
for(int j = 0; j < 10; j++
{
//执行代码
}
}
首先是进行最外层的循环,当i=0时,执行内层的循环j从0到9执行了10次,然后是i=2,执行内层的循环j从0到9执行了10次...


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="topic.aspx.cs" Inherits="JoyOne.Web.topic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DataList嵌套使用</title>
</head>
<body>
<form id="peak" runat="server">
<div class="container">
<--!外层Datalist-->
<asp:DataList ID="dlCategory" runat="server" RepeatColumns="1" OnItemDataBound="dlCategory_ItemDataBound"
CellPadding="4" ForeColor="#333333">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<--!内层Datalist-->
<div class="Category">
<div class="CategoryNameBorder">
<asp:Label ID="lblClassifyId" Text='<%# DataBinder.Eval(Container.DataItem ,"ProductClassifyID") %>'
runat="server" Visible="False"></asp:Label>
<div class="left">
<asp:Label ID="lblClassifyName" class="ClassifyNameBackGround" runat="server" Text='<%# DataBinder.Eval(Container.DataItem ,"ClassifyName") %>'></asp:Label>
</div>
<div class="right">
<a href='product-orderball-list.aspx?classify_id=<%# DataBinder.Eval(Container.DataItem ,"ProductClassifyID") %>'>
更多>></a>
</div>
</div>
<asp:DataList ID="dlTopicDetail" runat="server" RepeatColumns="5" RepeatDirection="Horizontal">
<ItemTemplate>
<div class="imagecontainer">
<a href='product-orderball-detail.aspx?product_id=<%# DataBinder.Eval(Container.DataItem ,"ProductID")%>'
target="_blank">
<img onload="rs(this)" src='<%# "Admin/UpLoad/"+ DataBinder.Eval(Container.DataItem, "ImagePath")%>'
alt='<%# DataBinder.Eval(Container.DataItem, "ProductName")%>' title='<%# DataBinder.Eval(Container.DataItem, "ProductName")%>' /></a>
</div>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
</asp:DataList>
</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using JoyOne.Framework;
namespace JoyOne.Web
{
public partial class topic : System.Web.UI.Page
{
private int nTopicID;
protected void Page_Load(object sender, EventArgs e)
{
nTopicID = Convert.ToInt32(Request["topic_id"]);
CategoryDataBind();
}
//外层Datalist数据绑定
private void CategoryDataBind()
{
DataTable dt = Product.GetProductClassifyList();
dlCategory.DataSource = dt;
dlCategory.DataBind();
}
//内层嵌套Datalist数据绑定
protected void dlCategory_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblClassifyId = (Label)e.Item.FindControl("lblClassifyId");
DataList dlTopicDetail = (DataList)e.Item.FindControl("dlTopicDetail");
DataTable dt = Product.GetProductListByTopicID(nTopicID, Convert.ToInt32(lblClassifyId.Text));
if (dt.Rows.Count > 0)
{
dlTopicDetail.DataSource = dt;
dlTopicDetail.DataBind();
}
else
{
Literal lt = new Literal();
lt.Text = "<div style='text-align:center; font:bold 12px/20px simsun; color:#f00;'>没有该主题的产品</div>";
e.Item.Controls.Add(lt);
}
}
}
}
}