datalist嵌套TreeView,并执行TreeView的选择事件。
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeDemo.aspx.cs" Inherits="TreeDemo" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:DataList ID="DataList1" runat="server"
onitemdatabound="DataList1_ItemDataBound">
<ItemTemplate>
aaaaaaaaaa
<asp:TreeView ID="tree" runat="server"
onselectednodechanged="tree_SelectedNodeChanged" ></asp:TreeView>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
后天代码
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class TreeDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
DataRow dr = dt.NewRow();
dr["name"] = "阿一";
dt.Rows.Add(dr);
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
protected void tree_SelectedNodeChanged(object sender, EventArgs e)
{
TreeView tv = (TreeView)sender;
Response.Write(tv.SelectedValue);
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemIndex != -1)
{
TreeView tv = (TreeView)e.Item.FindControl("tree");
TreeNode node = new TreeNode();
node.Text = "测试";
node.Value = "1";
tv.Nodes.Add(node);
}
}
}