TreeView树型菜单添加,修改,删除,绑定DropDownList功能

原创 2007年09月18日 18:34:00
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)
  {//
移动 
   //
此功能实现的时候需要考虑移动中所有类的深度是否有子类问题
  }
 
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

jQuery 树型菜单插件(Treeview)

jQuery 树型菜单插件(Treeview) jQuery Treeview 提供了一个无序灵活的可折叠的树形菜单。试用于一些菜单的导航,支持基于 cookie 的持久性菜单。 i...

TreeView树形菜单绑定

前台:

使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中。添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)

-----------前台:-------------------                 &nbsp;书名:   ...

C# TreeView树型菜单控件

  • 2009-02-26 14:56
  • 69KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)