TreeView 节点的显示,读取,操作

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]

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

上一篇:【原创】纯干货,Spring-data-jpa详解,全方位介绍。(转)


下一篇:struts.xml详细配置