1、ASPNET 进阶TreeView控件学习【ASP.NET 进阶】TreeView控件学习using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.Security;using System.Data;using Yc_TestS.BaseClass;namespace Yc_TestS public partial class TreeViewTest : System.Web.UI.Pa
2、ge protected void Page_Load(object sender, EventArgs e) /* * TreeNodeBinding类在TreeView控件中定义数据项与该数据项绑定到的节点之间的关系。 * 该类的DataMember属性指定在节点显示的数据源对应XML的节点。 * ValueField属性对应TreeNode对象的Value属性。 * TreeNodeBinding类的Text属性指定向用户显示的文本, 如果该属性没有指定,则默认与ValueField属性相同。 * */ this.treeT.ShowLines = false;/显示连接子节点和父节点之
3、间的线条 TreeNodeBinding Area = new TreeNodeBinding(); Area.DataMember = Area;/指定绑定的成员 Area.ValueField = cAreaName;/取值的字段 this.treeT.DataBindings.Add(Area); TreeNodeBinding Province = new TreeNodeBinding(); Province.DataMember = Province;/添加与省份绑定 Province.ValueField = cAreaName; this.treeT.DataBindings.
4、Add(Province); TreeNodeBinding City = new TreeNodeBinding(); City.DataMember = City;/添加与城市绑定 City.ValueField = cAreaName; this.treeT.DataBindings.Add(City); 4.后台数据库数据填充方式a.数据库数据b.前台页面 TreeView function show(msg) alert(msg); c.后台代码using System;using System.Collections.Generic;using System.Web;using S
5、ystem.Web.UI;using System.Web.UI.WebControls;using System.Web.Security;using System.Data;using Yc_TestS.BaseClass;namespace Yc_TestS public partial class TreeViewTest : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) AddTree(0, null); private void AddTree(int Pid, TreeNode PN
6、ode) string sqlStr = SELECT Item_id,Item_name,Parent_ID FROM Para_item; DataTable dt = YSqlHelper.ExecuteQuery(sqlStr, CommandType.Text); if (dt.Rows.Count 0) DataView dv = new DataView(dt); /过滤ParentID,得到当前的所有子节点 ParentID为父节点ID dv.RowFilter = Parent_ID = + Pid; /循环递归 foreach (DataRowView Row in dv)
7、 /声明节点 TreeNode Node = new TreeNode(); /绑定超级链接 Node.NavigateUrl = String.Format(javascript:show(0), RowItem_Name.ToString(); /开始递归 if (PNode = null) /添加根节点 Node.Text = RowItem_Name.ToString(); treeT.Nodes.Add(Node); Node.Expanded = true; /节点状态展开 AddTree(Int32.Parse(RowItem_ID.ToString(), Node); /再次递
8、归 else /添加当前节点的子节点 Node.Text = RowItem_Name.ToString(); PNode.ChildNodes.Add(Node); Node.Expanded = true; /节点状态展开 AddTree(Int32.Parse(RowItem_ID.ToString(), Node); /再次递归 d.配置文件 e.两个助手类using System;using System.Collections.Generic;using System.Web;using System.IO;using System.Security.Cryptography;us
9、ing System.Text;namespace Yc_TestS.BaseClass public class EntryCode / 加密 public static string EncodeCode(string str) string key = david.yc; DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8); provider.IV = Encoding.ASCII.Get
10、Bytes(key.Substring(0, 8); byte bytes = Encoding.GetEncoding(GB2312).GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringB
11、uilder builder = new StringBuilder(); foreach (byte num in stream.ToArray() builder.AppendFormat(0:X2, num); stream.Close(); return builder.ToString().Trim(); / Des 解密 GB2312 public static string DecodeCode(string str) string key = david.yc; DESCryptoServiceProvider provider = new DESCryptoServicePr
12、ovider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8); byte buffer = new bytestr.Length / 2; for (int i = 0; i (str.Length / 2); i+) int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10); bufferi = (byte)num2; MemoryStream
13、 stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); stream.Close(); return Encoding.GetEncoding(GB2312).GetString(stream.ToArray().Trim(); using System;u
14、sing System.Collections.Generic;using System.Web;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace Yc_TestS.BaseClass public class YSqlHelper private static string connectionString = EntryCode.DecodeCode(ConfigurationManager.AppSettingssqlStr);/获取解密后的数据库连接字符串 / 执行不带参
15、数的增删改SQL语句或存储过程 / 增删改SQL语句或存储过程 / 命令类型 / 受影响的行数 public static int ExecuteNonQuery(string cmdText, CommandType ct) int res = 0; using (SqlConnection conn = new SqlConnection(connectionString) try conn.Open(); using (SqlCommand cmd = new SqlCommand(cmdText, conn) cmd.CommandType = ct; res = cmd.Execut
16、eNonQuery(); catch finally if (conn.State = ConnectionState.Open) conn.Close(); return res; / 执行带参数的增删改SQL语句或存储过程 / 增删改SQL语句或存储过程 / 命令类型 / 受影响的行数 public static int ExecuteNonQuery(string cmdText, SqlParameter paras, CommandType ct) int res = 0; using (SqlConnection conn = new SqlConnection(connectio
17、nString) try conn.Open(); using (SqlCommand cmd = new SqlCommand(cmdText, conn) cmd.CommandType = ct; cmd.Parameters.AddRange(paras); res = cmd.ExecuteNonQuery(); catch finally if (conn.State = ConnectionState.Open) conn.Close(); return res; / 执行带参数的查询SQL语句或存储过程 / 查询SQL语句或存储过程 / 参数集合? / 命令类型 / DataT
18、able对象 public static DataTable ExecuteQuery(string cmdText, CommandType ct) DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString) try conn.Open(); using (SqlCommand cmd = new SqlCommand(cmdText, conn) cmd.CommandType = ct; using (SqlDataReader sdr = cmd.Exec
19、uteReader(CommandBehavior.CloseConnection) dt.Load(sdr); catch finally if (conn.State = ConnectionState.Open) conn.Close(); return dt; / 执行带参数的查询SQL语句或存储过程 / 查询SQL语句或存储过程 / 参数集合? / 命令类型 / DataTable对象 public static DataTable ExecuteQuery(string cmdText, SqlParameter paras, CommandType ct) DataTable d
20、t = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString) try conn.Open(); using (SqlCommand cmd = new SqlCommand(cmdText, conn) cmd.CommandType = ct; cmd.Parameters.AddRange(paras); using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection) dt.Load(sdr)
21、; catch finally if (conn.State = ConnectionState.Open) conn.Close(); return dt; 5.一些方法记录a.控制父结点的开/合状态TreeView_Tree.GetNodeFromIndex(1).Expanded=false/true;b.清除某个父结点下的所有子节点TreeView_IndexLeft.GetNodeFromIndex(1).Nodes.Clear();c.设置所选节点,如选中第二个节点function SetSelNode() TreeView_Tree.selectedNodeIndex=1; d.
22、得到所选节点的Text,ID或NodeDatafunction GetAttribute() alert(TreeView_Tree.getTreeNode(TreeView_Tree.selectedNodeIndex).getAttribute(Text); /替换Text为ID或NodeData,可分别得到所选节点的ID或NodeDatae.修改节点属性,如修改第一个节点的Textfunction ModifyNode() var node=TreeView_Tree.getTreeNode(0); node.setAttribute(Text,第一个节点); f.添加节点function AddNode() var node=TreeView_Tree.createTreeNode(); node.setAttribute(Text,第一个节点); TreeView_Tree.add(node); g.TreeView 中的SelectedIndexChange不执行AutoPostBack=true,SelectedIndexChange才能被执行。h.判断 TreeView 的一个节点下是否有子节点if(SelectNode.Nodes.Count=0) /该节点没有字节点
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1