哪位大虾有从数据库中调用数据并绑定到treeview控件上的例子,是用C#写的
解决方案 »
- 正则匹配问题
- 遇到一个非常奇怪的问题,哪位大侠能够解释一下,谢谢!
- 求解:虚拟路径 "...ascx" 映射到另一个应用程序,这是不允许的。
- 100分求专家门给我网站点意见
- 请问如何使用ASP.NET制作ASP网站的流量访问啊!
- 怎么这么简单也有错误,那位兄弟帮我看看!!!
- 从数据库中读取IMG并生成图片(问题解决马上给分,顶者有分,来者看清要求)
- 跨工程填充报表数据,项目卡死,高手帮忙!
- GridView在做行编辑的时候和删除的时候出现索引超出范围。必须为非负值并小于集合大小。
- asp.net还分c#,vb.net,j#这些类型吗?
- vc#.net项目里这样的xx.aspx文件是怎样建立的
- 为什么我的session十几分钟就过期?而且有时长有时短?
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data .SqlClient;
using Microsoft.Web.UI.WebControls;
using System.Configuration;
using NewWork;
namespace km_new.bulletin.metting
{
/// <summary>
/// select_user 的摘要说明。
/// </summary>
public class select_user: System.Web.UI.Page
{
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack )
{
createroot(); //创建树
adduser(); //树中加用户
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void createroot() //创建根节点
{
SqlConnection myConnection;
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
SqlDataAdapter myCommand = new SqlDataAdapter("select dep_name,dep_id from sys_dep_master", myConnection);
DataSet ds = new DataSet(); //取得所有群组名(根)
myCommand.Fill(ds, "t1"); //保存sys_dep_master的所有dep_name,dep_id
for(int i=0;i<=ds.Tables [0].Rows.Count -1;i++)
{
TreeNode t1=new TreeNode ();
t1.ImageUrl =@"../../image/tree/root.gif";
t1.ID=ds.Tables [0].Rows [i][1].ToString ();
t1.Text =ds.Tables [0].Rows [i][0].ToString (); //取得所有dep_name和id
TreeView1.Nodes.Add(t1); //所有群组名加到根目录 string id=t1.ID ;
InitTree(t1.Nodes,id); //调用InitTree函数 形成所有子树
}
}
private void InitTree(TreeNodeCollection Nds,string masterId)
{
string str;
SqlConnection myConnection;
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
str="select * from sys_dep_chi where master_dep_id="+"'"+masterId+"'";
SqlDataAdapter myCommand = new SqlDataAdapter(str, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t1"); for(int i=0;i<=ds.Tables ["t1"].Rows.Count-1;i++)
{
TreeNode tmpNd=new TreeNode();
tmpNd.ID =ds.Tables ["t1"].Rows[i][0].ToString ();
tmpNd.Text =ds.Tables ["t1"].Rows[i][1].ToString ();
tmpNd.ImageUrl =@"../../image/tree/root1.gif";
Nds.Add (tmpNd);
InitTree(tmpNd.Nodes,tmpNd.ID); //递归调用 }
} private void adduser() //添加用户
{
GetAllNode(TreeView1.Nodes); }
void GetAllNode(TreeNodeCollection tnc) //遍历树的每个节点
{
foreach(TreeNode node in tnc)
{
if(node.Nodes.Count!=0)
GetAllNode(node.Nodes);
adduser1(node.Nodes ,node.ID ); //每个节点的nodes集合和id作为参数,构造树
} } private void adduser1(TreeNodeCollection tnc,string id) //每个节点添加用户
{
SqlConnection myConnection;
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
SqlDataAdapter myCommand = new SqlDataAdapter("select user_name,a.user_id,dep_id from sys_user as a,sys_user_dep as b where a.user_id=b.user_id", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t1"); for(int i=0;i<=ds.Tables ["t1"].Rows.Count -1;i++)
if(id ==ds.Tables ["t1"].Rows[i][2].ToString ())
{
TreeNode t1=new TreeNode ();
t1.ImageUrl =@"../../image/tree/person.gif";
t1.ID =ds.Tables ["t1"].Rows[i][1].ToString ();
t1.Text =ds.Tables [0].Rows [i][0].ToString ();
tnc.Add (t1); }
}
哪位大虾给我发个完整的例子,我的有限[email protected]