1、我们先在页面中布置好两个DataList。并分别起名,dlsProductMenu,dlsProductInfo。如下:
(1)为主DataList绑定主数据
DataList嵌套的重点是要在外层DataList的ItemDataBound事件中完成对嵌套DataList的绑定。在主DataList的 ItemDataBound事件中用e.Item.FindControl方法来找到嵌套层DataList的id,完后为该id绑定数据。比如:
private
void
dlsProductMenu_ItemDataBound(
object
sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dataList = (DataList) e.Item.FindControl("dlsProductInfo");
DataRowView rowv = (DataRowView)e.Item.DataItem;
int mainID = Convert.ToInt32(rowv["Id"]);
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
if(mainID > 0)
{
DataSet ds = SqlDataProvider.GetSubContent(mainID);//获取从数据。
if(ds != null)
{
try
{
dataList.DataSource = ds;
dataList.DataBind();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
<
TABLE
id
="Table3"
cellSpacing
="0"
cellPadding
="0"
width
="90%"
align
="center"
border
="0"
>
< TBODY >
< TR >
< TD vAlign ="top" >
<!-- 主菜单开始 -->
< ASP:datalist id ="dlsProductMenu" runat ="server" RepeatDirection ="Horizontal" RepeatColumns ="2" Width ="100%" >
< ItemTemplate >
< TABLE class ="table02" id ="Table13" cellSpacing ="0" cellPadding ="0" width ="100%" align ="center" border ="0" >
< TR bgColor ="#f5f5f5" >
< TD width ="50%" height ="30" >
<% # DataBinder.Eval(Container.DataItem, " Name " ) %>
</ TD >
</ TR >
</ TABLE >
<!-- 子菜单开始 -->
< ASP:datalist id ="dlsProductInfo" Width ="100%" runat ="server" RepeatColumns ="1" >
< ItemTemplate >
< TABLE class ="table02" id ="Table14" cellSpacing ="0" cellPadding ="0" width ="100%" align ="center" border ="0" >
< TR bgColor ="#f5f5f5" >
< TD width ="50%" height ="30" >
< DIV class ="style10" align ="left" > 标题: < a href =../../'ProductInfoDetail.ASPx?subID=<%# DataBinder.Eval(Container.DataItem,"subID") % > '> <% # DataBinder.Eval(Container.DataItem, " Title " ) %>
</ a >
</ DIV >
</ TD >
</ TR >
</ TABLE >
</ ItemTemplate >
</ ASP:datalist >
<!-- 子菜单结束 -->
</ ItemTemplate >
</ ASP:datalist >
<!-- 主菜单结束 --> </ TD >
</ TR >
</ TBODY >
</ TABLE >
2、进行数据绑定。
< TBODY >
< TR >
< TD vAlign ="top" >
<!-- 主菜单开始 -->
< ASP:datalist id ="dlsProductMenu" runat ="server" RepeatDirection ="Horizontal" RepeatColumns ="2" Width ="100%" >
< ItemTemplate >
< TABLE class ="table02" id ="Table13" cellSpacing ="0" cellPadding ="0" width ="100%" align ="center" border ="0" >
< TR bgColor ="#f5f5f5" >
< TD width ="50%" height ="30" >
<% # DataBinder.Eval(Container.DataItem, " Name " ) %>
</ TD >
</ TR >
</ TABLE >
<!-- 子菜单开始 -->
< ASP:datalist id ="dlsProductInfo" Width ="100%" runat ="server" RepeatColumns ="1" >
< ItemTemplate >
< TABLE class ="table02" id ="Table14" cellSpacing ="0" cellPadding ="0" width ="100%" align ="center" border ="0" >
< TR bgColor ="#f5f5f5" >
< TD width ="50%" height ="30" >
< DIV class ="style10" align ="left" > 标题: < a href =../../'ProductInfoDetail.ASPx?subID=<%# DataBinder.Eval(Container.DataItem,"subID") % > '> <% # DataBinder.Eval(Container.DataItem, " Title " ) %>
</ a >
</ DIV >
</ TD >
</ TR >
</ TABLE >
</ ItemTemplate >
</ ASP:datalist >
<!-- 子菜单结束 -->
</ ItemTemplate >
</ ASP:datalist >
<!-- 主菜单结束 --> </ TD >
</ TR >
</ TBODY >
</ TABLE >
(1)为主DataList绑定主数据
DataSet ds
=
SqlDataProvider.GetMainInfo();
//
获取将要绑定的数据。
this .dlsProductMenu.DataSource = ds;
this .dlsProductMenu.DataBind();
(2)为子DataList绑定从数据。这也是该文的重点。
this .dlsProductMenu.DataSource = ds;
this .dlsProductMenu.DataBind();
DataList嵌套的重点是要在外层DataList的ItemDataBound事件中完成对嵌套DataList的绑定。在主DataList的 ItemDataBound事件中用e.Item.FindControl方法来找到嵌套层DataList的id,完后为该id绑定数据。比如:
![](https://i-blog.csdnimg.cn/blog_migrate/17c135a2763225eb253f4ff92e88a664.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/353c1fa9cd42287d30310af820b5430d.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0263e0c3f9f3faecf5b4177968c5d64c.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0263e0c3f9f3faecf5b4177968c5d64c.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0263e0c3f9f3faecf5b4177968c5d64c.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0263e0c3f9f3faecf5b4177968c5d64c.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6b1eff4d9cb8055b7f54d7b6d5b73a91.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0263e0c3f9f3faecf5b4177968c5d64c.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/258e3e26538340eb1c68b7657f2a2de8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6b1eff4d9cb8055b7f54d7b6d5b73a91.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6b1eff4d9cb8055b7f54d7b6d5b73a91.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6b1eff4d9cb8055b7f54d7b6d5b73a91.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6b1eff4d9cb8055b7f54d7b6d5b73a91.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6bfb8366602b2effefcf17e2749f1731.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/17c135a2763225eb253f4ff92e88a664.gif)