62,182
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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.Xml;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataView dv = drv("select * from lname");
foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text = datav["lname"].ToString();
tn.Value = datav["lid"].ToString();
tn.Expanded = false;
TreeView1.Nodes.Add(tn);
Addchildnodes(tn);
}
}
}
private void Addchildnodes(TreeNode tn)
{
string lid = Convert.ToString(tn.Value);
DataView dv = drv("select * from xname where lid="+lid+"");
foreach (DataRowView datav in dv)
{
TreeNode ctn = new TreeNode();
ctn.Text = datav["xname"].ToString();
ctn.Value = datav["id"].ToString();
tn.ChildNodes.Add(ctn);
}
}
private DataView drv(string query)
{
SqlConnection mycon = new SqlConnection("server=.;uid=sa;pwd=Ysin2004;database=test");
SqlDataAdapter sda = new SqlDataAdapter(query, mycon);
DataSet ds = new DataSet();
sda.Fill(ds, "tree");
return ds.Tables["tree"].DefaultView;
}
}
码