treeview 控件动态绑定数据部分实例代码
一:
============================================================================
treeview 源教程实例部分代码段
============================================================================
protected void page_load(object o ,eventargs e )
{//首先填充第一级节点
dataview dv=获取dataview视图函数;
foreach(datarowview datav in dv)
{//循环数据视图,将对应的值交给一个节点对象,然后添加到树上有继续添加
treenode tn=new TreeNode();
tn.Text=datav("title").ToString();
tn.Value=datav("bid").ToString();
tn.Expanded=false;
TreeView1.Nodes.Add(tn);
AddchildNodes(tn);//调用方法填充二级节点
}
}
private void AddchildNodes(TreeNode tn)
{//填充二级节点,他需要一级节点的value ,所以干脆送了一个完整的根节点对象
int bid=Convert.ToInt32(tn.Value);
DataView dv=drv("select * from class where bid="+bid+");
foreach(DataRowView datav in dv)
{//同上
TreeNode ctn=new TreeNode();
ctn.Text=datav["title"].ToString();
ctn.Value=datav["cid"].ToString();
ctn.NavigateUrl="http://thcjp.cnblogs.com/jiashe.aspx?aa="+datav["cid"].ToSring;
tn.ChildNodes.Add(ctn);
//把节点ctn加载到tn下面或者说添加为tn的子节点
}
}
//下面这段代码的作用在于把选用节点的text值打印出来
protect void Button1_Click(object sender,Eventargs e)
{
if(TreeView1.CheckedNodes.Count>0)
{
//如果有项被选中
foreach(TreeNode tn in TreeView1.CheckedNodes)
{//跌代所有被选中的节点
if(tb.ChildNodes.Count>0)
{//如果被选中项下面还有子节点
foreach(TreeNode ctn in tn.ChildNodes)
{//迭代所有子节点,把值读出来
Response.Write(ctn.Text+"<br>");
}
}
else
{
//如果下面没有节点了,说明是叶节点,那么直接读出被选取项的值
Response.Write(tn.Text+"<br>");
}
}
}
}
============================================================================
treeview 实例代码段(待续)
============================================================================
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
{
TreeNode tn = new TreeNode();
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.ShowLines = true;
TreeView1.ToolTip = "a test";
TreeView1.ShowExpandCollapse = true;
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
if (!IsPostBack)
{
tn.Text = "根结点";
tn.Value = "rootnode";
tn.Expanded = true;
TreeView1.Nodes.Add(tn);
addlevel2nodes(tn);
}
else
{
addlevel2nodes(tn);
}
}
protected void addlevel2nodes(TreeNode fathertn)
{
string conn = "Server=localhost;Database=bookshop;Uid=sa;Pwd=asen";
Userinfo userinfo1 = new Userinfo(conn);
DataView dv1 = userinfo1.selectalluserinfo();
foreach (DataRowView dv in dv1)
{
TreeNode tn = new TreeNode();
tn.Text = dv["username"].ToString();
tn.Value = dv[3].ToString();
tn.Expanded = true;
fathertn.ChildNodes.Add(tn);
}
}
}
方法 二
注意在设计数据库时,ID为自动增长,ROWID 是基于父节点ID的子节点
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.SqlClient;
public partial class TreeView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitTreeView();
TreeView1.Target = "main"; //框架的NAME
}
}
private void InitTreeView()
{
string strSqlSelete = "SELECT ID,NAME,URL,ROWID,PIC FROM TREE1";
DataView dataview = GetTreeDataView(strSqlSelete);
dataview.RowFilter = "ROWID=0";
foreach (DataRowView AddDataView in dataview)
{
TreeNode node = new TreeNode();
node.Text = AddDataView["NAME"].ToString();
node.Value = AddDataView["ID"].ToString();
node.NavigateUrl = AddDataView["URL"].ToString();
node.ImageUrl = AddDataView["PIC"].ToString();
node.ImageToolTip = "首页图象";
TreeView1.Nodes.Add(node);
AddChildNodes(node);
}
}
private DataView GetTreeDataView(string strSQL)
{
ConnClass conn = new ConnClass();
SqlConnection Conn = new SqlConnection(conn.GetStrConnction);
SqlDataAdapter da = new SqlDataAdapter(strSQL, Conn);
DataSet ds = new DataSet();
da.Fill(ds, "TreeView");
return ds.Tables["TreeView"].DefaultView;
}
private void AddChildNodes(TreeNode ChildNode)
{
int ROWID = Convert.ToInt32(ChildNode.Value);
DataView dataview = GetTreeDataView("SELECT ID,NAME,URL,PIC FROM TREE1 WHERE ROWID=" + ROWID);
foreach (DataRowView AddChilDataView in dataview)
{
TreeNode Node = new TreeNode();
Node.Text = AddChilDataView["NAME"].ToString();
Node.Value = AddChilDataView["ID"].ToString();
Node.NavigateUrl = AddChilDataView["URL"].ToString();
Node.ImageUrl = AddChilDataView["PIC"].ToString();
Node.ImageToolTip = AddChilDataView["NAME"].ToString();
Node.ToolTip=AddChilDataView["NAME"].ToString();
ChildNode.ChildNodes.Add(Node);
AddChildNodes(Node);
}
}
}
方法三
TreeView绑定省市的代码。
=============================================
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{//首先填充第一级节点。
DataView dv = GetDv("select * from prov");
foreach(DataRowView datav in dv)
{ //循环数据视图,将对应的数据交给一个节点对象,然后添加到树上继续填加
TreeNode tn = new TreeNode();
tn.Text = datav["prov"].ToString();
tn.Value = datav["provid"].ToString();
tn.Expanded = false;
TreeView1.Nodes.Add(tn);
AddChildNode(tn);
}
}
}
private DataView GetDv(string sql)
{//根据传来的sql语句返回一个DataView数据式图
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath("~/") + @"//App_Data//data.mdb;Persist Security Info=True";
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Tree");
return ds.Tables["Tree"].DefaultView;
}
private void AddChildNode(TreeNode tn)
{//填充二级节点,需要一级节点的Value,所以直接传了个TreeNode
int provID = Convert.ToInt32(tn.Value);
DataView dv = GetDv("select * from city where provid=" + provID + "");
foreach (DataRowView datav in dv)
{
TreeNode Ctn = new TreeNode();
Ctn.Text = datav["city"].ToString();
Ctn.Value = datav["cityId"].ToString();
Ctn.NavigateUrl = "Detail.aspx?id="+datav["cityid"].ToString()+"";
Ctn.Target = "_blank";
tn.ChildNodes.Add(Ctn);
}
}