using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Tree1 : System.Web.UI.Page
{

protected System.Data.SqlClient.SqlConnection SqlConnection1;
    protected System.Data.SqlClient.SqlDataAdapter SqlDataAdapter1;
    protected System.Data.DataSet DataSet1;
    protected System.Data.DataSet DataSet2;
    string Str_sql = "";
    static string Str_selected = "";  // 选中的值
   
   
    protected void Page_Load(object sender, EventArgs e)
    {
        AddTree(0, null);
    }

private void AddTree(int Pid, TreeNode PNode)
    {
        //string sqlStr = "SELECT Item_id,Item_name,Parent_ID FROM Para_item";
        //DataTable dt = YSqlHelper.ExecuteQuery(sqlStr, CommandType.Text);

if (!IsPostBack)
        {

P_ht.H_data P_data = new P_ht.H_data();
            P_data.sql_text1 = "SELECT Item_id,Item_name,Parent_ID FROM Para_item";
            Str_sql = P_data.sql_text1;
            P_data.select_tables();
            DataSet1 = P_data.DataSet8;
            DataTable dt = DataSet1.Tables[0];

if (dt.Rows.Count > 0)
            {
                DataView dv = new DataView(dt);
                //过滤ParentID,得到当前的所有子节点 ParentID为父节点ID
                dv.RowFilter = "[Parent_ID] = " + Pid;
                //循环递归
                foreach (DataRowView Row in dv)
                {
                    //声明节点
                    TreeNode Node = new TreeNode();
                    //绑定超级链接
                    // Node.NavigateUrl = String.Format("javascript:show('{0}')", Row["Item_Name"].ToString());
                    //开始递归
                    if (PNode == null)
                    {
                        //添加根节点
                        Node.Text = Row["Item_Name"].ToString();
                        this.TreeView1.Nodes.Add(Node);
                        Node.Expanded = true; //节点状态展开
                        AddTree(Int32.Parse(Row["Item_ID"].ToString()), Node);    //再次递归
                    }
                    else
                    {
                        //添加当前节点的子节点
                        Node.Text = Row["Item_Name"].ToString();
                        PNode.ChildNodes.Add(Node);
                        Node.Expanded = true; //节点状态展开
                        AddTree(Int32.Parse(Row["Item_ID"].ToString()), Node);     //再次递归
                    }
                }
            }
        }
    }

////
    // 选中节点
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        //this.Label1.Text = TreeView1.SelectedNode.Value.ToString().Trim();
        //this.Label1.Text = TreeView1.SelectedNode.Text.ToString().Trim();
        //Response.Write(TreeView1.SelectedNode.Value.ToString().Trim());
        Str_selected = TreeView1.SelectedNode.Value.ToString().Trim();
        Response.Write(Str_selected);
    }

// add
    protected void Btn_node_add_Click(object sender, EventArgs e)
    {
       
        P_ht.H_data P_data = new P_ht.H_data();
        if (Str_selected.Trim() == "")
        {
            ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
            scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 选中的节点为空,您要重新点击节点才能选中节点! ');", true);
            return;
        }
        else
        {
            string str_Parent_ID = "0";
            Int32 i_Item_id = 0;

//select
            P_data.sql_text1 = " SELECT Item_id,Item_name,Parent_ID FROM Para_item where Item_name = '" + Str_selected.Trim().ToString() +"' ";
            Str_sql = P_data.sql_text1;
            P_data.select_tables();
            DataSet1 = P_data.DataSet8;
            if (DataSet1.Tables[0].Rows.Count > 0)
            {
                str_Parent_ID = DataSet1.Tables[0].Rows[0][0].ToString().Trim();

P_data.sql_text1 = " SELECT Item_id,Item_name,Parent_ID FROM Para_item order by Item_id desc ";
                Str_sql = P_data.sql_text1;
                P_data.select_tables();
                DataSet1 = P_data.DataSet8;

i_Item_id =  System.Convert.ToInt32(DataSet1.Tables[0].Rows[0][0].ToString().Trim())+1;
            }
            else
            {
                ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
                scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 没有找到节点编号, 不能增加节点! ');", true);
                return;
            }

//add
            P_data.p11_count = "4";  //总数量+1

P_data.p11_ID = i_Item_id.ToString().Trim(); //编号
            P_data.p11_01 = "月当楼";
            P_data.p11_02 = str_Parent_ID;

P_data.p21_ID = "t3_01";
            P_data.p21_01 = "t3_02";
            P_data.p21_02 = "t3_03";

P_data.sql_text1 = "INSERT INTO Para_item (Item_id,Item_name,Parent_ID) VALUES "
                                               + "(@t3_01, @t3_02, @t3_03)";

P_data.write_tables();
        }

////reshfer;

//TreeNode nodeNew = new TreeNode("月当楼");// 新建节点对象

//if (TreeView1.SelectedNode == null)
        //{
        //    return;
        //}
        //else
        //{
        //    TreeView1.SelectedNode.ChildNodes.Add(nodeNew);
        //}

Server.Transfer("Tree1.aspx");

}

protected void Btn_node_edit_Click(object sender, EventArgs e)
    {
        P_ht.H_data P_data = new P_ht.H_data();
        if (Str_selected.Trim() == "")
        {
            ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
            scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 选中的节点为空,您要重新点击节点才能选中节点! ');", true);
            return;
        }
        else
        {
           
            //select

string str_Parent_ID = "";

P_data.sql_text1 = " SELECT Item_id,Item_name,Parent_ID FROM Para_item where Item_name = '" + Str_selected.Trim().ToString() + "' ";
            Str_sql = P_data.sql_text1;
            P_data.select_tables();
            DataSet1 = P_data.DataSet8;
            if (DataSet1.Tables[0].Rows.Count > 0)
            {
                str_Parent_ID = DataSet1.Tables[0].Rows[0][0].ToString().Trim();
                Session["Tree1_Edit_name"] = str_Parent_ID;
            }
            else
            {
                ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
                scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 没有找到节点编号, 不能删除节点! ');", true);
                return;
            }

//原窗口保留,以对话框形式打开新窗口:
            //Response.Write("<script>window.showModelessDialog('Tree1_edit.aspx')</script>");
            //return;
           
            P_data.sql_text1 = "update Para_item set Item_name = '户部巷' "
                                                  + " where Item_id =  " + str_Parent_ID.Trim().ToString() + " ";

P_data.update_tables();
        }

////reshfer;

//if (TreeView1.SelectedNode == null)
        //{
        //    return;
        //}
        //else
        //{
        //    TreeView1.SelectedNode.Value = "户部巷";
        //    TreeView1.SelectedNode.Text = "户部巷";
        //}

Server.Transfer("Tree1.aspx");
    }

// delete
    protected void Btn_node_delete_Click(object sender, EventArgs e)
    {
        P_ht.H_data P_data = new P_ht.H_data();
        if (Str_selected.Trim() == "")
        {
            ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
            scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 选中的节点为空,您要重新点击节点才能选中节点! ');", true);
            return;
        }
        else
        {
            //select

string str_num_ID = "";
            string str_Parent_ID = "";

P_data.sql_text1 = " SELECT Item_id,Item_name,Parent_ID FROM Para_item where Item_name = '" + Str_selected.Trim().ToString() + "' ";
            Str_sql = P_data.sql_text1;
            P_data.select_tables();
            DataSet1 = P_data.DataSet8;
            if (DataSet1.Tables[0].Rows.Count > 0)
            {
                str_num_ID = DataSet1.Tables[0].Rows[0][0].ToString().Trim();
                str_Parent_ID = DataSet1.Tables[0].Rows[0][2].ToString().Trim();
            }
            else
            {
                ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
                scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 没有找到节点编号, 不能删除节点! ');", true);
                return;
            }

// 如果有子节点提示先删除子节点,否则不予删除。( 另一种方法是写代码,用递归的方法找到所有的子节点,批量性删除 )
            P_data.sql_text1 = " SELECT Item_id,Item_name,Parent_ID FROM Para_item where Parent_ID = '" + str_num_ID.Trim().ToString() + "' ";
            Str_sql = P_data.sql_text1;
            P_data.select_tables();
            DataSet1 = P_data.DataSet8;
            if (DataSet1.Tables[0].Rows.Count > 0)
            {
                ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
                scriptManager.RegisterStartupScript(typeof(string), "", "alert('提示: 不能删除, 先删除所有子节点后才都能删除父节点! ');", true);
                return;
            }
            else
            {
                P_data.sql_text1 = " delete from Para_item where Item_id = " + str_num_ID.Trim().ToString() + " ";
                P_data.update_tables();
            }
        }

//reshfer;
        Server.Transfer("Tree1.aspx");

}

}

******************************************************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tree1.aspx.cs" Inherits="Tree1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TreeView1 数据库测试</title>
    <script type="text/javascript">
        function show(msg) {
            alert(msg);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
         <br />
        <br />
   
        <asp:Panel ID="Panel1" runat="server" BackColor="#FDF8CA" BorderStyle="None"
            Height="370px">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Btn_node_add" runat="server" onclick="Btn_node_add_Click"
                Text="Add" Width="76px" />
            &nbsp;
            <asp:Button ID="Btn_node_edit" runat="server" onclick="Btn_node_edit_Click"
                Text="Edit" Width="76px" />
            &nbsp;
            <asp:Button ID="Btn_node_delete" runat="server" onclick="Btn_node_delete_Click"
                Text="Delete" Width="76px" />
            <br />
            <br />
                    <asp:TreeView ID="TreeView1" runat="server"
                onselectednodechanged="TreeView1_SelectedNodeChanged">
                    </asp:TreeView>
            <br />
            <br />
        </asp:Panel>
       
        <br />
        <br />
   
    </div>
    </form>
</body>
</html>

*************************************************************

CREATE TABLE [dbo].[Para_item](
 [Item_id] [int] NULL,
 [Item_name] [nvarchar](50) NULL,
 [Parent_ID] [nvarchar](50) NULL
) ON [PRIMARY]

*************************************************************

TreeView 节点的显示,读取,操作的更多相关文章

  1. 动态载入TreeView时让TreeView节点前显示加号

    解释下标题,我这里通过webservice获取数据并动态载入TreeView节点.那么某个节点展开前它是没有子节点的.那么它就不显示加号.这样会让用户误以为此节点不能展开.我是这样做的,每次创建节点a ...

  2. TreeView节点拖拉操作1

      TreeView节点拖拉操作1 //事先设置 TreeView1.DragMode= dmAutomatic;   unit Unit1; interface uses   Windows, Me ...

  3. WINFORM中treeview 节点显示不全

    在设置treeview节点时,出现如下显示不全的问题: 这个问题是由于我们在treeview任务中编辑节点时设置的字体大于我们在treeview属性中设置frot字体导致的. 所以只要将treevie ...

  4. DSAPI TreeView节点增删简化操作

    将一行或多行字符串添加到TreeView控件. 函数: 控件.TreeView控件.添加路径节点到TreeView(要添加节点的TreeView控件,字符串或字符串数组) 示例:将一个多行文本添加到T ...

  5. TreeView节点

    TreeView由节点构成,建树通过对TreeView.items属性进行操作.Items是一个TTreeNodes对象,这是一个TTreeNode集. 一.针对TTreeNodes,也就是 Tree ...

  6. SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题

    转:http://blog.csdn.net/miragesky2049/article/details/7204882 SharePoint2010沙盒解决方案基础开发--关于TreeView树形控 ...

  7. 20151124002 treeView 数型菜单的操作

    20151124002 treeView 数型菜单的操作 protected void FillTree()        {            SqlConnection1 = new Syst ...

  8. Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置

    QtreeView是ui中最常用的控件,Qt中QTreeWidget比QTreeView更简单,但没有QTreeView那么灵活(QTreeWidget封装的和MFC的CTreeCtrl很类似,没有m ...

  9. WPF:使用Json.NET在TreeView中树形显示JSON数据

    原文 WPF:使用Json.NET在TreeView中树形显示JSON数据 据 读者可以参考这个开源的可以树形显示XML和JSON的工具: Mgen Object 603:XML/JSON树形显示小工 ...

随机推荐

  1. Save ITCM

    Debug String if below 64 BYTE it will in DRAM or it will in ITCM So to save ITCM , move it to ALLStr ...

  2. JSON 字符串 与 java 对象的转换

    jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //j ...

  3. 锋利的jQuery

    今天总要找点东西学习,其实有很多东西要记录,慢慢写,今天看书吧,这几天把这本书看完,这里记一些要点 从头开始记吧 第一章 认识jQuery $就是jQuery的简写 $(function(){}) 就 ...

  4. 基于Qt实现的截图小程序

    在最近做的行人检测项目中,由于需要训练分类器,而分类器的训练又需要有一个一定长宽的样本.为了方便样本的采集,因此实现了这样的一个截图程序.该程序的主要功能是加载视频到程序中,程序可以对视频进行播放.暂 ...

  5. HTML调用servlet(一)

    1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...

  6. :“boost/serialization/string.hpp”: No such file or directory 错误

    主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html

  7. ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析

    配置内容分析如下 # -- 定义上下文使用者 -- security.context =default # -- 定义密码限制长度最小值 -- password.length.min =5 # -- ...

  8. android Activity的启动模式与flag的见解

    最近做一个安卓项目,想要实现的效果就是:当打开一个按钮的时候,启动了一个A功能,当用户返回到桌面再继续进去的时候,不过之前在哪个Activity,都会先跳转到A功能的那个界面,当用户点击返回的时候,再 ...

  9. react 绑定事件

    1.显示隐藏 2.输入框输入内容,立即显示出来 代码如下: 注意:版本 React v15.0.1 ReactDOM v15.0.1 browser.min.js是编译文件,将代码解析为浏览器识别的j ...

  10. JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总

    一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出.       1,JSTL标签总结: a).JSTL标签有什么用?          JSTL是由JCP(Java Commu ...