TreeView的使用(在下没有积分了,但我跪求了,谢谢了)
- 提问者网友:像風在裏
- 2021-05-04 00:31
- 五星知识达人网友:愁杀梦里人
- 2021-05-04 01:37
要动态链接数据库的还是静态的啊??
==================================================================================
以下是动态链接数据库的,是我以前在网上摘抄的,但是网址给忘了,静态的话用treeview的子节点(也就是你要加的节点号).ChildNodes.Add(新的节点序号)就OK了
==================================================================================
首先建立一个bests数据库,其中有worker、department、company三张表,结构如下面三张图所示:
这三张表可以描述一个公司的结构。其中后两张表的manager与worker表中useid关联。
本程序的功能是将公司人员结构表示出来
类:db.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;
using System.Data.SqlClient;
/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
protected string constr;
private int pagesize;
public db(string dbstr)
{
constr="Data Source=.;Initial Catalog="+dbstr+";Integrated Security=True";
pagesize = 4;
}
public DataView sdt(string queue)
{
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter(queue, con);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
return ds.Tables["name"].DefaultView ;
}
}
前台程序
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Simple" NodeIndent="10"
ShowCheckBoxes="All">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#DD5555" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="0px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
<br />
<br />
</div>
</form>
</body>
后台程序
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
db sdb = new db("bests");
DataView dv = sdb.sdt("select * from company");
foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text =datav ["name"].ToString ();
tn.Value = datav["companyid"].ToString();
tn.Expanded = false ;
TreeView1.Nodes.Add(tn);
addchildnode(tn);
}
}
}
private void addchildnode(TreeNode tf)
{
string id = tf.Value;
db sdb = new db("bests");
DataView dv = sdb.sdt("select * from department where companyid='"+id +"'");
foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text = datav["departname"].ToString();
tn.Value = datav["departid"].ToString();
tn.Expanded = false;
tf.ChildNodes .Add(tn);
childnode(tn);
}
}
private void childnode(TreeNode tf)
{
string id = tf.Value;
db sdb = new db("bests");
DataView dv = sdb.sdt("select * from worker where departid='" + id + "'");
foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text = datav["name"].ToString();
tn.Value = datav["useid"].ToString();
tn.NavigateUrl = "~/default3.aspx?aa=" + datav["departid"].ToString();
tn.Expanded = false;
tf.ChildNodes.Add(tn);
}
}
}