DataList嵌套的重点是要在外层DataList的ItemDataBound事件中用e.Item.FindControl方法来找到嵌套层DataList的id,编写嵌套层DataList的绑定数据源事件,具体代码如下:
'--------前台代码DLNested.aspx:
<%@ Page language="c#" Codebehind="DLNested.aspx.cs" AutoEventWireup="false" Inherits="Example.DateListNested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DateListNested</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style type="text/css">TD { FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: "宋体" }
</style>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1"
cellPadding="1" width="100%" border="1" bordercolordark=white>
<tr>
<td><a href="Default.aspx">DataList分页、编辑实例</a></td>
</tr>
<TR>
<TD>
<asp:DataList id="DataList1" runat="server" RepeatColumns="3">
<ItemTemplate>
<FONT face="宋体">
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolordark=white>
<TR>
<TD><FONT face="宋体" color="red">
<asp:Label id=Label1 runat="server" Text='<%# Container.ItemIndex + ":" + DataBinder.Eval(Container.DataItem,"C_TypeName")%>' >
</asp:Label>
<asp:Label id="lblTypeId" runat="server" Visible=False Text='<%# DataBinder.Eval(Container.DataItem,"I_TypeID")%>' >
</asp:Label>
</FONT></TD>
</TR>
<TR>
<TD>
<asp:DataList id="DataList2" runat="server">
<ItemTemplate>
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolordark=white>
<TR>
<TD>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"C_Name") %>'>
</asp:Label></TD>
</TR>
</TABLE>
<FONT face="宋体"></FONT>
</ItemTemplate>
<AlternatingItemTemplate>
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolordark=white>
<TR>
<TD><FONT face="宋体" color=#003333>
<asp:Label id="Label6" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"C_Name") %>'>
</asp:Label></font></TD>
</TR>
</TABLE>
<FONT face="宋体"></FONT>
</AlternatingItemTemplate>
</asp:DataList></TD>
</TR>
</TABLE>
</FONT>
</ItemTemplate>
<AlternatingItemTemplate>
<FONT face="宋体">
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolordark=white>
<TR>
<TD><FONT face="宋体" color="red">
<asp:Label id=Label3 runat="server" Text='<%# Container.ItemIndex + ":" + DataBinder.Eval(Container.DataItem,"C_TypeName") %>'>
</asp:Label>
<asp:Label id="lblTypeID1" runat="server" Visible=False Text='<%# DataBinder.Eval(Container.DataItem,"I_TypeID")%>' >
</asp:Label>
</FONT></TD>
</TR>
<TR>
<TD>
<asp:DataList id="Datalist3" runat="server">
<ItemTemplate>
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolordark=white>
<TR>
<TD>
<asp:Label id="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"C_Name") %>'>
</asp:Label></TD>
</TR>
</TABLE>
<FONT face="宋体"></FONT>
</ItemTemplate>
<AlternatingItemTemplate>
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolordark=white>
<TR>
<TD><FONT face="宋体" color="#003333">
<asp:Label id="Label5" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"C_Name") %>'>
</asp:Label></font></TD>
</TR>
</TABLE>
<FONT face="宋体"></FONT>
</AlternatingItemTemplate>
</asp:DataList></TD>
</TR>
</TABLE>
</FONT>
</AlternatingItemTemplate>
</asp:DataList></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
'--------后台代码DLNested.aspx.cs:
/*----------------------------------------------------------------
// 版权所有: 亿思维软件工作室
// 网站地址: Http://www.easewe.com
// 联系方式: 87015203(QQ),easewe@126.com(Email)
// 整理日期: 2005.09.14
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Example
{
/// <summary>
/// DateListNested 的摘要说明。
/// </summary>
public class DateListNested : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected string connString;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
connString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Request.PhysicalApplicationPath + "DataList.mdb";
if ( !Page.IsPostBack )
{
BindData();
//BindArticle();
}
}
private void BindData()
{
string commandText = "select * from ArticleType";
//----------
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(commandText,conn);
DataSet ds = new DataSet();
da.Fill(ds,"ArticleType");
conn.Close();
//----------
DataList1.DataSource = ds.Tables["ArticleType"];
DataList1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
DataList dl = null;
Label lblTypeID = null;
if ( e.Item.ItemType == ListItemType.Item )
{
dl = (DataList)e.Item.FindControl("DataList2");
lblTypeID = (Label)e.Item.FindControl("lblTypeID");
}
else
{
dl = (DataList)e.Item.FindControl("DataList3");
lblTypeID = (Label)e.Item.FindControl("lblTypeID1");
}
string typeID = lblTypeID.Text.ToString();
string commandText = "select * from Article";
commandText = commandText + " Where I_TypeID=" + typeID;
//------------
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(commandText,conn);
DataSet ds = new DataSet();
da.Fill(ds,"Article");
conn.Close();
//------------
dl.DataSource = ds.Tables["Article"];
dl.DataBind();
}
}
}
}
本文引用通告地址: http://blog.csdn.net/gdeljyh/services/trackbacks/481490.aspx