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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{//首先填充第一级节点
DataView dv = drv("select * from big");
foreach (DataRowView data in dv)
{//循环数据视图,将对应的值交给第一个节点对象,然后添加到树上有继续添加
TreeNode tn = new TreeNode();
tn.Text = Datav["title"].ToString();
tn.Text = dtaav["bid"].ToString();
tn.Expanded = false;
TreeView1.Nodes.Add(tn);
AddchildNodes(tn);//调用方法填充二级节点
}
}
}
protected void AddchildNodesc(TreeNode tn)
{//填充二级节点,需要一级节点的value,所以干脆送一个完整的根节点对象
int bid = Convert.ToInt32(tn.Value);
DataView dv = drv("select * from class where bid=" + bid + "");
foreach (DataRow datav in dv)
{//同上
TreeNode tn = new TreeNode();
tn.Text = Datav["title"].ToString();
tn.Text = dtaav["bid"].ToString();
ctn.NavigateUrl = "http://thcip.cnblogs.com/jiashe.aspx?aa=" + dtav["cid"].Totn.ChilaNodes.Add(ctn);
}
}
private DataView drv(string query)
{//根据传来的SQL语句串返回一个数据图
string connstr = ConfigurationManager.ConnectionStrings["aaConn"].ConnectionString;
SqlConnection con = new SqlConnection(connstr);
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
sda.Fill(ds, "tree");
return ds.Tables["tree"].DefaultView;
}
protected void Button1_Click(object sender, EventArgs e)
{//将选项中的文本值打印出来
if (TreeView1.CheckedNodes.Count > 0)
{//如果有项被选中
foreach (TreeNode tn in TreeView1.CheckedNodes)
{//跌代所有被选中的节点
if (tn.ChildNodes.Count > 0)
{//如果被选中项下面还有子节点
foreach (TreeNode ctn in tn.ChildNodes)
{//跌代所有子节点,把值读出来
Response.Write(ctn.Text +"<br>");
}
}
else
{//如果下面没有节点了,说明是页节点,那么直接读出来被选项的值
Rwsponse.Write(tn.Text + "<br>");
}
}
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{//点击了没有连接的节点(前提是起selectAction属性为 select)
Label1.Text = TreeView1.SelectedNode.Text;
}
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
}
protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
{
Label1.Text = "你折叠了" + e.Node.Text;
}
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
Label1.Text = "你展开了" + e.Node.Text;
}
}