ASP.NET TreeView树型菜单操作实例(代码调试通过)

本实例完成TreeView树型菜单添加,修改,删除,绑定DropDownList功能,移动功能读者可以自行书写
.aspx代码

<form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <TABLE id="Table1" cellSpacing="1" cellPadding="1"

width="700" border="1">
     <TR>
      <TD style="WIDTH: 168px"></TD>
      <TD>TreeView操作实例</TD>
     </TR>
     <TR>
      <TD style="WIDTH: 168px" vAlign="top">
       <iewc:TreeView id="TreeView1"

runat="server"></iewc:TreeView></TD>
      <TD vAlign="top">
       <TABLE id="Table2" cellSpacing="1"

cellPadding="1" width="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="WIDTH: 168px"></TD>
      <TD></TD>
     </TR>
    </TABLE>
   </FONT>
  </form>

.aspx.cs后台代码

protected Microsoft.Web.UI.WebControls.TreeView TreeView1; 
  public string returnvalue="";//递归存储值

 
  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);
   DropDownListBind(0,DropDownList1);

   //添加根节点
   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 DropDownListBind(int parterid,DropDownList droplist){//递归类别
   DataSet ds=returnDataset(parterid);
   if(ds!=null){
    for(int i=0;i<ds.Tables[0].Rows.Count;i++){
     ListItem list=new ListItem();
     if (ds.Tables[0].Rows[i]["depth"].ToString()=="0")
     {
      list.Text=ds.Tables[0].Rows[i]

["name"].ToString();
     }
     else{
      string depth="";
      //for(i=1;i<Int32.Parse(ds.Tables[0].Rows

[i]["depth"].ToString());i++){
       depth+="--";
      //}
          list.Text=depth+ds.Tables

[0].Rows[i]["name"].ToString();
     }
     list.Value=ds.Tables[0].Rows[i]["id"].ToString();
     droplist.Items.Add(list);
     DataSet ds2=returnDataset(Int32.Parse(ds.Tables

[0].Rows[i]["id"].ToString()));
     if(ds2.Tables[0].Rows.Count>0){
      DropDownListBind(Int32.Parse(ds.Tables

[0].Rows[i]["id"].ToString()),DropDownList1);
     }
    }
   }
  }

  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);
   //Response.Write(id.ToString()+"<br>");
   string tmpid="";
   tmpid=id.ToString()+allid(treenode.Nodes).ToString();
   //Response.Write(tmpid);
   //Response.End();
   if (treenode.Target!="0")
   {
    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="delete * from treeview where id in

("+tmpid+")";
    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("不能删除根节点");    
   }
  }

  private string allid(TreeNodeCollection tnc)
  {//遍历TreeView节点(递归算法)
   foreach(TreeNode node in tnc){
    if(node.Nodes.Count!=0){
     returnvalue+=","+node.Target;
     //Response.Write(node.Text+node.Target+"<br>");
     allid(node.Nodes);     
    }
    else{
     //Response.Write(node.Text+node.Target+"<br>");
     returnvalue+=","+node.Target;
    }
   }
   return returnvalue;
  }

  private void moveButton_Click(object sender, System.EventArgs e)
  {//移动 
   //此功能实现的时候需要考虑移动中所有类的深度是否有子类问题
  }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值