ASP.NET - TreeView控件,只操作最后一级节点
效果:
使用母板页进行,左右页面进行跳转。
绑定TreeView控件:http://www.cnblogs.com/KTblog/p/4792302.html
主要功能:
- 点击节点的时候,只操作最后一级的节点,其他节点跳过不执行代码。
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Web.UI.WebControls; 6 7 namespace APManage 8 { 9 public partial class TreeView : System.Web.UI.Page 10 { 11 public string ConnString = @"server = HUANGFU-PC; database = DB_APManage; integrated security = true"; 12 public DataTable dt = null; 13 public List<int> erjinode = new List<int>(); //存储非最后一级的节点 14 15 /// <summary> 16 /// 初始化事件 17 /// </summary> 18 /// <param name="sender"></param> 19 /// <param name="e"></param> 20 protected void Page_Load(object sender, EventArgs e) 21 { 22 if (!IsPostBack) 23 { 24 dt = new DataTable(); 25 26 GetMenuToDataTable("select * from Tb_APCategory", dt); 27 BindTree(dt, null, "1000"); 28 } 29 } 30 31 /// <summary> 32 /// 选择存储所有的非最后一级节点的ID 33 /// </summary> 34 public void SelectIsNotMainNode() 35 { 36 SqlConnection con = new SqlConnection(ConnString); 37 con.Open(); 38 SqlCommand cmd = new SqlCommand("select ID from Tb_APCategory where ParentID = 1000", con); 39 SqlDataReader sdr = cmd.ExecuteReader(); 40 while (sdr.Read()) 41 { 42 SqlConnection con_2 = new SqlConnection(ConnString); 43 con_2.Open(); 44 SqlCommand cmd_2 = new SqlCommand("select ID from Tb_APCategory where ParentID = " + sdr["ID"].ToString() + "", con_2); 45 SqlDataReader sdr_2 = cmd_2.ExecuteReader(); 46 while (sdr_2.Read()) 47 { 48 erjinode.Add(Convert.ToInt32(sdr_2["ID"].ToString())); 49 } 50 erjinode.Add(Convert.ToInt32(sdr["ID"].ToString())); 51 } 52 } 53 54 /// <summary> 55 /// 鼠标点击节点事件 56 /// </summary> 57 /// <param name="sender"></param> 58 /// <param name="e"></param> 59 protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) 60 { 61 string node = this.TreeView1.SelectedNode.Value;//获得所选择节点的值,也就是表中的ID 62 SelectIsNotMainNode(); 63 foreach (int item in erjinode) 64 { 65 if (node == item.ToString()) 66 { 67 ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('不是最后一个节点!')</script>"); 68 return; 69 } 70 } 71 ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('"+node+"!')</script>"); 72 } 73 74 #region 绑定TreeView控件 75 76 /// <summary> 77 /// 循环绑定TreView 78 /// </summary> 79 /// <param name="dtSource"></param> 80 /// <param name="parentNode"></param> 81 /// <param name="parentID"></param> 82 private void BindTree(DataTable dtSource, TreeNode parentNode, string parentID) 83 { 84 DataRow[] rows = dtSource.Select(string.Format("ParentID={0}", parentID)); 85 foreach (DataRow row in rows) 86 { 87 TreeNode node = new TreeNode(); 88 node.Text = row["CategoryName"].ToString(); 89 node.Value = row["ID"].ToString(); 90 BindTree(dtSource, node, row["ID"].ToString()); 91 if (parentNode == null) 92 { 93 this.TreeView1.Nodes.Add(node); 94 } 95 else 96 { 97 parentNode.ChildNodes.Add(node); 98 } 99 } 100 } 101 102 /// <summary> 103 /// 绑定DataTable 104 /// </summary> 105 /// <param name="query"></param> 106 /// <param name="dt"></param> 107 /// <returns></returns> 108 private DataTable GetMenuToDataTable(string query, DataTable dt) 109 { 110 using (SqlConnection conn = new SqlConnection(ConnString)) 111 { 112 SqlCommand cmd = new SqlCommand(query, conn); 113 SqlDataAdapter ada = new SqlDataAdapter(cmd); 114 ada.Fill(dt); 115 } 116 return dt; 117 } 118 119 #endregion 120 } 121 }
posted on 2015-09-16 22:36 ultrastrong 阅读(479) 评论(0) 编辑 收藏 举报