在开发过程中,经常会遇到嵌套的问题,以下是一个datalist的三层嵌套的例子:
前台*.aspx页面

<%...@ Page language="c#" Codebehind="datalist_qiantao_1.aspx.cs" AutoEventWireup="false" Inherits="csharp_test_1.datalist_qiantao" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>datalist_qiantao</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="default.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="DataList1" runat="server" Width="100%" DataKeyField="系统代码" RepeatColumns="0">
<ItemTemplate>
<table width="100%">
<tr>
<td>

<img src="images/2.gif" id="img1"></asp:ImageButton><%...#DataBinder.Eval(Container.DataItem,"系统名称")%>
<HR align="left">
</td>
</tr>
<tr>
<td>
<asp:DataList id="DataList2" runat="server" DataKeyField="子系统代码" OnItemDataBound="DataList2_ItemDataBound">
<ItemStyle Font-Size="12pt"></ItemStyle>
<ItemTemplate>
<font color="red">

<%...#DataBinder.Eval(Container.DataItem,"子系统名称")%>
</font>
<br>
<asp:DataList id="Datalist3" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" DataKeyField="程式代码"
OnItemDataBound="DataList3_ItemDataBound">
<ItemStyle Width="300px" HorizontalAlign="Left" Font-Size="9pt"></ItemStyle>
<ItemTemplate>
<asp:HyperLink id="HyperLink1" runat="server" Target="_blank">

<%...#DataBinder.Eval(Container.DataItem,"程式名称")%>
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>
<br>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
</form>

<SCRIPT LANGUAGE="JavaScript">...
var TB = document.getElementsByTagName("img")
var TR
for(i=0;i<TB.length;i++)

...{
TR = TB[i]
TR.style.cursor="hand";
TR.attachEvent("onclick",show)
}
function show()

...{
//var imgUrl=new Array();
// imgUrl[0]="images/1.gif"
//imgUrl[1]="images/2.gif"
var TD = event.srcElement;
//var node = document.getElementsByName(TD)
var src = TD.getAttribute("src")
var TB = TD.parentElement;
while(TB.tagName != "TABLE")

...{
TB = TB.parentElement
}
var TR = TB.rows[1]
if(TR.style.display == "none")

...{
TR.style.display = ""
//document.getElementsByName(TD).src=imgUrl[0]
TD.setAttribute("src","images/2.gif")
}
else

...{
TR.style.display = "none"
TD.setAttribute("src","images/1.gif")
}
}
</SCRIPT>
</body>
</HTML>

后台*.cs页面:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace csharp_test_1

...{

/**//// <summary>
/// datalist_qiantao 的摘要说明。
/// </summary>
public class datalist_qiantao : System.Web.UI.Page

...{
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.DataList DataList1;
public string strR

...{

get ...{return (string)ViewState["strR"];}

set ...{ViewState["strR"]=value;}
}
private void Page_Load(object sender, System.EventArgs e)

...{
if(!IsPostBack)

...{
DataListDataBind();
}
}


Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)

...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

...{
//this.DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_ItemCommand);
this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataListDataBind()

...{
SqlConnection conn = new SqlConnection("server=172.23.128.214;user=DataUser;pwd=datauserzixun;database=acfv3");
SqlCommand sqlcmd=new SqlCommand("select distinct 系统代码,系统名称 from acf_v_lefttree_Admin_1", conn);
conn.Open();
DataList1.DataSource = sqlcmd.ExecuteReader();
DataList1.DataBind();
conn.Close();
}

protected void DataList2_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)

...{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

...{
DataList dlDir2=(DataList)sender;
DataList dlDir3=(DataList)e.Item.FindControl("DataList3");
SqlConnection conn = new SqlConnection("server=172.23.128.214;user=DataUser;pwd=datauserzixun;database=acfv3");
conn.Open();
string aa=dlDir2.DataKeys[e.Item.ItemIndex].ToString().Trim();
SqlCommand sqlcmd=new SqlCommand("select distinct 程式代码,程式名称 from acf_v_lefttree_Admin_1 where 子系统代码='" + aa + "' order by 程式代码", conn);
dlDir3.DataSource = sqlcmd.ExecuteReader();
dlDir3.DataBind();
conn.Close();

}

}

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)

...{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

...{
DataList dlDir2=(DataList)e.Item.FindControl("DataList2");
SqlConnection conn = new SqlConnection("server=172.23.128.214;user=DataUser;pwd=datauserzixun;database=acfv3");
conn.Open();
string aa=DataList1.DataKeys[e.Item.ItemIndex].ToString().Trim();
SqlCommand sqlcmd=new SqlCommand("select distinct 子系统代码,子系统名称 from acf_v_lefttree_Admin_1 where 系统代码='" + aa + "' order by 子系统代码", conn);
dlDir2.DataSource = sqlcmd.ExecuteReader();
dlDir2.DataBind();
conn.Close();
}
}

protected void DataList3_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)

...{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

...{
DataList dlDir3=(DataList)sender;
string ProgramID=dlDir3.DataKeys[e.Item.ItemIndex].ToString().Trim();
if ((ProgramID.Substring(1,2)=="01" && Convert.ToInt32(ProgramID.Substring(2,2)) < 13)
||(ProgramID.Substring(1,2)=="04" && Convert.ToInt32(ProgramID.Substring(2,2)) < 43)
||(ProgramID.Substring(1,2)=="05" && Convert.ToInt32(ProgramID.Substring(2,2)) < 53)
||(ProgramID.Substring(1,2)=="07" && Convert.ToInt32(ProgramID.Substring(2,2)) < 73)
||(ProgramID.Substring(1,2)=="08" && Convert.ToInt32(ProgramID.Substring(2,2)) < 85)
||(ProgramID.Substring(1,2)=="31" && Convert.ToInt32(ProgramID.Substring(2,2)) < 12)
||(ProgramID.Substring(1,2)=="32" && Convert.ToInt32(ProgramID.Substring(2,2)) < 22)
)

...{
this.strR="http://172.23.128.191/Webacf.com.v3/WebacfPublicProgram/SearchPage.aspx?ProgramID="+ProgramID;
}
HyperLink hp1=(HyperLink)e.Item.FindControl("HyperLink1");
hp1.NavigateUrl=this.strR;
}
}
}
}

总结:DataList子控件事件的激发应用客户端激发更为合理