private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
dt = new MapHelp().GetAreaTreeView("0");
if (!IsPostBack)
{
TreeNode tnode = new TreeNode();
tnode.Text = "中國";
tnode.Value = "1";
tnode.ToolTip = "1";
HouseTree.Nodes.Add(tnode);//添加根節點
GetTreeData(tnode);//添加下一層節點
}
detailDiv.InnerHtml = detailHidden.Value;
}
private void GetTreeData(TreeNode node)
{
DataRow[] dr = dt.Select("up_id=1");
foreach (DataRow mydr in dr)
{
TreeNode tnode = new TreeNode();
tnode.Text = mydr["area_nm"].ToString().Trim();
tnode.Value = mydr["area_id"].ToString().Trim();
tnode.ToolTip = tnode.Value;
node.ChildNodes.Add(tnode);
//AddHouseTreeChildNodes(dt, tnode);//此句放開則加載所有節點
//tnode.Expanded = false;
}
}
private void AddHouseTreeChildNodes(DataTable dt ,TreeNode node)
{
DataRow[] dr = dt.Select("up_id=" + node.Value);
foreach (DataRow mydr in dr)
{
TreeNode childNode = new TreeNode();
childNode.Text = mydr["area_nm"].ToString().Trim();
childNode.Value = mydr["area_id"].ToString().Trim();
node.ChildNodes.Add(childNode);
AddHouseTreeChildNodes(dt, childNode);
}
}
protected void HouseTree_SelectedNodeChanged(object sender, EventArgs e)
{
detailDiv.InnerHtml = "";
HouseTree.SelectedNode.ChildNodes.Clear();//清空節點以免重複添加節點
AddHouseTreeChildNodes(dt, HouseTree.SelectedNode);
DataTable mydt = new MapHelp().GetHouseInformation(HouseTree.SelectedNode.Value);
if (dt != null && dt.Rows.Count > 0)
{
AddTreeFileData(mydt, HouseTree.SelectedNode);
HouseTree.SelectedNode.Expanded = true;
}
//明細資料顯示
DataTable detailTab = new MapHelp().GetHouseInformation(HouseTree.SelectedNode.Value);
if (detailTab != null && detailTab.Rows.Count > 0)
{
}
}
private void AddTreeFileData(DataTable mydt, TreeNode node)
{
foreach (DataRow dr in mydt.Rows)
{
TreeNode childNode = new TreeNode();
childNode.Text = dr["room_nm"].ToString().Trim();
childNode.Value=dr["room_id"].ToString().Trim();
childNode.ImageUrl = "../images/page_white_text.png";
childNode.ToolTip = dr["room_id"].ToString().Trim();
node.ChildNodes.Add(childNode);
childNode.SelectAction = TreeNodeSelectAction.None;
}
}
可以參考下上面的代碼,下加載2層,然後點擊那層加載那層,