DropDownList多级树
DropDownList多级树
Cate.xml文件内容
面页Default.aspx内容
Default.aspx.cs内容
App_Code/CateDAL.cs文件
Cate.xml文件内容
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Content>
<ID>1</ID>
<ParentID>0</ParentID>
<CateName>类别1</CateName>
</Content>
<Content>
<ID>2</ID>
<ParentID>1</ParentID>
<CateName>类别2</CateName>
</Content>
<Content>
<ID>3</ID>
<ParentID>2</ParentID>
<CateName>类别3</CateName>
</Content>
<Content>
<ID>4</ID>
<ParentID>3</ParentID>
<CateName>类别4</CateName>
</Content>
<Content>
<ID>5</ID>
<ParentID>0</ParentID>
<CateName>类别5</CateName>
</Content>
<Content>
<ID>6</ID>
<ParentID>0</ParentID>
<CateName>类别6</CateName>
</Content>
<Content>
<ID>7</ID>
<ParentID>6</ParentID>
<CateName>类别7</CateName>
</Content>
<Content>
<ID>8</ID>
<ParentID>7</ParentID>
<CateName>类别8</CateName>
</Content>
<Content>
<ID>9</ID>
<ParentID>0</ParentID>
<CateName>类别9</CateName>
</Content>
<Content>
<ID>10</ID>
<ParentID>0</ParentID>
<CateName>类别10</CateName>
</Content>
</Root>
<Root>
<Content>
<ID>1</ID>
<ParentID>0</ParentID>
<CateName>类别1</CateName>
</Content>
<Content>
<ID>2</ID>
<ParentID>1</ParentID>
<CateName>类别2</CateName>
</Content>
<Content>
<ID>3</ID>
<ParentID>2</ParentID>
<CateName>类别3</CateName>
</Content>
<Content>
<ID>4</ID>
<ParentID>3</ParentID>
<CateName>类别4</CateName>
</Content>
<Content>
<ID>5</ID>
<ParentID>0</ParentID>
<CateName>类别5</CateName>
</Content>
<Content>
<ID>6</ID>
<ParentID>0</ParentID>
<CateName>类别6</CateName>
</Content>
<Content>
<ID>7</ID>
<ParentID>6</ParentID>
<CateName>类别7</CateName>
</Content>
<Content>
<ID>8</ID>
<ParentID>7</ParentID>
<CateName>类别8</CateName>
</Content>
<Content>
<ID>9</ID>
<ParentID>0</ParentID>
<CateName>类别9</CateName>
</Content>
<Content>
<ID>10</ID>
<ParentID>0</ParentID>
<CateName>类别10</CateName>
</Content>
</Root>
面页Default.aspx内容
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<asp:DropDownList ID="ddlCate" runat="server">
</asp:DropDownList></div>
</form>
</body>
</html>
<!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>
<asp:DropDownList ID="ddlCate" runat="server">
</asp:DropDownList></div>
</form>
</body>
</html>
Default.aspx.cs内容
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData(0, "",0);
}
}
private void BindData(int pId,string textStr,int m)
{
DataTable dt= CateDAL.GetData(pId,Server.MapPath("Cate.xml"));
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
textStr = "";
if (pId != 0)
{
m = m + 1;
for (int j = 0; j < m; j++)
{
textStr += " ";
}
}
else
{
m = 0;
}
int parantID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
ddlCate.Items.Add(new ListItem(textStr + "├" + dt.Rows[i]["CateName"].ToString(), parantID.ToString()));
BindData(parantID, textStr, m);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData(0, "",0);
}
}
private void BindData(int pId,string textStr,int m)
{
DataTable dt= CateDAL.GetData(pId,Server.MapPath("Cate.xml"));
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
textStr = "";
if (pId != 0)
{
m = m + 1;
for (int j = 0; j < m; j++)
{
textStr += " ";
}
}
else
{
m = 0;
}
int parantID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
ddlCate.Items.Add(new ListItem(textStr + "├" + dt.Rows[i]["CateName"].ToString(), parantID.ToString()));
BindData(parantID, textStr, m);
}
}
}
}
App_Code/CateDAL.cs文件
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class CateDAL
{
private CateDAL()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static DataTable GetData(int parantID,string Path)
{
XmlUtility xmlData = new XmlUtility(Path);
DataSet ds = xmlData.GetData("Root/Content[ParentID=\"" + parantID + "\"]");
if (ds.Tables.Count != 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
}
XmlUtility.cs文件就是介绍我的博客上有using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class CateDAL
{
private CateDAL()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static DataTable GetData(int parantID,string Path)
{
XmlUtility xmlData = new XmlUtility(Path);
DataSet ds = xmlData.GetData("Root/Content[ParentID=\"" + parantID + "\"]");
if (ds.Tables.Count != 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
}