首页 > 编程 > .NET > 正文

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

2024-07-10 13:05:48
字体:
来源:转载
供稿:网友

本实例完成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 ></td>
      <td>treeview操作实例</td>
     </tr>
     <tr>
      <td 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 ></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)
  {//移动 
   //此功能实现的时候需要考虑移动中所有类的深度是否有子类问题
  }


菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表