.aspx代码
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="1" cellPadding="1"
;700" border="1">
<TR>
<TD style=" 168px"></TD>
<TD>TreeView操作实例</TD>
</TR>
<TR>
<TD style=" 168px" vAlign="top">
<iewc:TreeView id="TreeView1"
runat="server"></iewc:TreeView></TD>
<TD vAlign="top">
<TABLE id="Table2" cellSpacing="1"
cellPadding="1" ;100%" border="0">
<TR>
<TD>节点名
<asp:TextBox
id="TextBox1" runat="server"></asp:TextBox></TD>
</TR>
<TR>
<TD>
<asp:Button
id="addButton" runat="server" Text="添 加"></asp:Button>
<asp:Button
id="editButton" runat="server" Text="修 改"></asp:Button>
<asp:Button
id="delButton" runat="server" Text="删 除"></asp:Button></TD>
</TR>
<TR>
<TD>移动节点</TD>
</TR>
<TR>
<TD>要移动到的节点
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList></TD>
</TR>
<TR>
<TD>
<asp:Button
id="moveButton" runat="server" Text="移 动"></asp:Button></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD style=" 168px"></TD>
<TD></TD>
</TR>
</TABLE>
</FONT>
</form>
.aspx.cs代码
using Microsoft.Web.UI.WebControls;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack){
Button del=new Button();
del=(Button)Page.FindControl("delButton");
del.Attributes.Add("onclick","return confirm('确定删除');");
showlist();
}
}
private void showlist(){
//绑定TreeView
//infobase io=new infobase();
DropDownList1.Items.Clear();
ListItem list=new ListItem();
list.Value="0";
list.Text="全部类别";
DropDownList1.Items.Add(list);
//添加根节点
TreeView1.Nodes.Clear();
TreeNode node=new TreeNode();
node.Text="类别列表";
node.Target="0";
node.Expanded=true;
TreeView1.Nodes.Add(node);
treeShow(0,node);
TreeView1.DataBind();
}
private void treeShow(int parterid,TreeNode treenode){//递归类别
//
DataSet ds=returnDataset(parterid);
if (ds!=null)
{
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
TreeNode node=new TreeNode();
node.Target=ds.Tables[0].Rows[i]["id"].ToString();
node.Text=ds.Tables[0].Rows[i]["name"].ToString();
treenode.Nodes.Add(node);
node.Expanded=true;
DataSet ds2=returnDataset(Int32.Parse(ds.Tables
[0].Rows[i]["id"].ToString()));
if (ds2.Tables[0].Rows.Count>0)
{
treeShow(Int32.Parse(ds.Tables[0].Rows[i]
["id"].ToString()),node);
}
}
}
}
private DataSet returnDataset(int parterid){
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data
source="+Server.MapPath("Data.mdb");
OleDbDataAdapter cmd;
DataSet ds=new DataSet();
try
{
conn.Open();
cmd=new OleDbDataAdapter("select * from treeview where
parterid="+parterid+" order by createtime asc",conn);
cmd.Fill(ds);
}
catch(Exception ex)
{
Response.Write(ex.ToString());
Response.End();
}
finally{
conn.Dispose();
}
return ds;
}
private void addButton_Click(object sender, System.EventArgs e)
{//添加节点
if (TextBox1.Text!="")
{
TreeNode treenode=TreeView1.GetNodeFromIndex
(TreeView1.SelectedNodeIndex);
string[] treedepth=treenode.GetNodeIndex().Split('.');
if(treenode.Target!=null)
{
int depth=treedepth.Length-1;//获得深度
int parterid=Int32.Parse(treenode.Target);//获得父类
ID
string classname=TextBox1.Text;//获得类名
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath
("Data.mdb");
conn.Open();
OleDbCommand cmd=new OleDbCommand();
cmd.CommandType=CommandType.Text;
cmd.Connection=conn;
cmd.CommandText="insert into treeview
(name,parterid,depth)values('"+classname+"',"+parterid+","+depth+")";
try
{
cmd.ExecuteNonQuery();
Response.Write("<script>alert('添加成
功');location.href='TreeViewStudy.aspx';</script>");
Response.End();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
Response.End();
}
finally
{
cmd.Dispose();
conn.Dispose();
}
}
else{
//Response.Write(treenode.Target+"123<br>");
}
}
else{
Response.Write("请填写类别并选择节点");
}
}
private void editButton_Click(object sender, System.EventArgs e)
{//修改节点
if (TextBox1.Text!="")
{
TreeNode treenode=TreeView1.GetNodeFromIndex
(TreeView1.SelectedNodeIndex);
int id=Int32.Parse(treenode.Target);
string classname=TextBox1.Text;
if(treenode.Target!=null)
{
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath
("Data.mdb");
conn.Open();
OleDbCommand cmd=new OleDbCommand();
cmd.CommandType=CommandType.Text;
cmd.Connection=conn;
cmd.CommandText="update treeview set
name='"+classname+"' where id="+id;
try
{
cmd.ExecuteNonQuery();
Response.Write("<script>alert('修改成
功');location.href='TreeViewStudy.aspx';</script>");
Response.End();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
Response.End();
}
finally
{
cmd.Dispose();
conn.Dispose();
}
}
else{}
}
else
{
Response.Write("请填写类别并选择节点");
}
}
private void delButton_Click(object sender, System.EventArgs e)
{//删除
TreeNode treenode=TreeView1.GetNodeFromIndex
(TreeView1.SelectedNodeIndex);
int id=Int32.Parse(treenode.Target);
if (treenode.Target!="0")
{
}
else{
Response.Write("不能删除根节点");
}
}
转载于:https://blog.51cto.com/515632/785527