ASP.NET - TreeView 增删

时间:2023-03-09 01:40:47
ASP.NET - TreeView 增删

效果:

ASP.NET - TreeView 增删

前端代码:

 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="APManage.Site1" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged ="TreeView1_SelectedNodeChanged" ShowLines="True">
</asp:TreeView>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick ="LinkButton1_Click">修改节点</asp:LinkButton>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

后端代码:

 using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using APManage.App_Code;
using System.Data.SqlClient; namespace APManage
{
public partial class updateNodes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.DropDownList1.DataSource = SQLHelper.ExecuteTable("select * from Tb_APCategory where ParentID = 1000", CommandType.Text);
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataTextField = "CategoryName";
this.DropDownList1.DataBind(); List<category> inof = new List<category>();
SqlDataReader sdr = SQLHelper.ExcuteReader("select ID, CategoryName from Tb_APCategory where ParentID = 1000", CommandType.Text);
while (sdr.Read())
{
SqlDataReader sdr_2 = SQLHelper.ExcuteReader("select ID, CategoryName from Tb_APCategory where ParentID = " + sdr["ID"].ToString() + "", CommandType.Text);
while (sdr_2.Read())
{
inof.Add(new category(sdr_2["ID"].ToString(), sdr_2["CategoryName"].ToString()));
}
} this.DropDownList2.DataSource = inof;
this.DropDownList2.DataValueField = "ID";
this.DropDownList2.DataTextField = "Name";
this.DropDownList2.DataBind(); //this.DropDownList2.DataSource = SQLHelper.ExecuteTable("select * from Tb_APCategory", CommandType.Text);
//this.DropDownList2.DataValueField = "";
//this.DropDownList2.DataTextField = "";
//this.DropDownList2.DataBind();
} public class category
{
public category(string id, string name)
{
Id = id;
Name = name;
} private string id; public string Id
{
get { return id; }
set { id = value; }
} private string name; public string Name
{
get { return name; }
set { name = value; }
}
}
}
}