树形问题.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using Com.Csddt.BBS.BLL;
public partial class Web_WebUserControl2 : System.Web.UI.UserControl
{
BBSModuleBLL bll = new BBSModuleBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataBase();
TreeView1.ShowLines = true;//显示连接父节点与子节点间的线条
TreeView1.ExpandDepth = 1;//控件显示时所展开的层数
}
}
public void BindDataBase()
{
////实例化SqlConnection对象
//OracleConnection sqlCon = new OracleConnection();
////实例化SqlConnection对象连接数据库的字符串
//sqlCon.ConnectionString = "Data Source=YXDB1;user id=csddt_new;Password=csddt_new;Pooling=true;Min Pool Size=3;Max Pool Size=200";
////实例化SqlDataAdapter对象
//OracleDataAdapter da = new OracleDataAdapter("Select mid, modulename,parentid From bbs_module where iscommon=1", sqlCon);
////实例化数据集DataSet
////下面的方法动态添加了TreeView的根节点和子节点
////设置TreeView的根节点
//DataTable table = new DataTable();
//da.Fill(table);
DataTable table = new DataTable();
table = bll.moduleList();
CreateTree(0, null, table, TreeView1);
}
public void CreateTree(int parentID, TreeNode node, DataTable table, TreeView treeView)
{
DataRow[] rows = table.Select(" parentid=" + parentID);
if (rows == null || rows.Length < 1) { return; }
//用foreach遍历dv
foreach (DataRow r in rows)
{
//第一次加载时为空
if (node == null)
{
//创建根节点
TreeNode root = new TreeNode();
root.Expanded = false;
//必须与数据库的对应
root.Text = r["modulename"].ToString();
root.Value = r["mid"].ToString();
root.NavigateUrl = "Default2.aspx?mid=" + root.Value;
//添加根节点
this.TreeView1.Nodes.Add(root);
//递归调用方法本身
CreateTree(int.Parse(r["mid"].ToString()), root, table, treeView);
}
else
{
//添加子节点
TreeNode childNode = new TreeNode();
childNode.Expanded = false;
childNode.Text = r["modulename"].ToString();
childNode.Value = r["mid"].ToString();
childNode.NavigateUrl = "Default2.aspx?mid=" + childNode.Value;
node.ChildNodes.Add(childNode);
CreateTree(int.Parse(r["mid"].ToString()), childNode, table, treeView);
}
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
string script = "";
if (Txtmodulename.Text == "")
{
script = "请输入要查询的模块名";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myscirpt", "alert('" + script + "');", true);
}
else
{
string model = Txtmodulename.Text;
DataTable db = bll.searchList(model);
if (db.Rows.Count> 0)
{
}
else
{
script = "没有该板块";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myscirpt", "alert('" + script + "');", true);
Txtmodulename.Text = "";
}
}
}
}