要实现用treeview绑定数据库以及checkbox联级选中获取值 看到好多都是js和jQuery的。
个人对jQuery不是很熟悉 所以用代码.cs代码写的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="300" valign="top" style="text-align: left"> <br /> <asp:TreeView ID="TreeView1" runat="server" Height="168px" ImageSet="Simple" Width="147px" ShowCheckBoxes="All" ShowLines="True" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged"> <ParentNodeStyle Font-Bold="False" /> <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" /> <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" /> <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" /> </asp:TreeView> </td> <td valign="top"> <asp:Button ID="Button1" runat="server" Text="获得值" OnClick="Button1_Click"/> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </td> </tr> </table>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Page_Load(object sender, EventArgs e) { TreeView1.EnableClientScript = true; TreeView1.Attributes.Add("onclick", "postBackByObject(event)"); if (!IsPostBack) { DataSet ds = new TaxiRadarBasicInfo.BLL.TaxiCityCode().GetRegionAllList(); TreeView treeview = new TreeView(); foreach (DataRow row in ds.Tables[0].Rows) { TreeNode trP = new TreeNode(); trP.Text = row["CityName"].ToString(); trP.Value = row["CityCode"].ToString(); //TreeCAdd(trP, row["CityCode"].ToString()); trP.Expanded = false; TreeView1.Nodes.Add(trP); TreeCAdd(trP); } } } public void TreeCAdd(TreeNode trP) { string CityCode = trP.Value; DataSet ds = new TaxiRadarBasicInfo.BLL.TaxiCityCode().GetCarRegionByTerminalSN(CityCode); foreach (DataRow row in ds.Tables[0].Rows) { TreeNode trC = new TreeNode(); trC.Text = row["Brand"].ToString(); trC.Value = row["TerminalSN"].ToString(); trP.ChildNodes.Add(trC); } } protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e) { RecursiveCheckCheckBox(e.Node, e.Node.Checked, true); TreeNode current = e.Node; while (!current.Checked && current.Parent != null && !IsChildNodesChecked(current.Parent)) { current.Parent.Checked = false; current = current.Parent; } if (e.Node.Checked) { RecursiveCheckCheckBox(e.Node, e.Node.Checked, false); } } private bool IsChildNodesChecked(TreeNode n) { bool result = false; if (n.ChildNodes.Count == 0) { result = n.Checked; } foreach (TreeNode t in n.ChildNodes) { if (result |= t.Checked) { break; } } return result; } private void RecursiveCheckCheckBox(TreeNode n, bool isChecked, bool topDown) { if (topDown) { foreach (TreeNode l in n.ChildNodes) { l.Checked = isChecked; if (l.ChildNodes.Count != 0) { RecursiveCheckCheckBox(l, isChecked, topDown); } } } else { n.Checked = isChecked; if (n.Parent != null) { RecursiveCheckCheckBox(n.Parent, isChecked, false); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Button1_Click(object sender, EventArgs e) { string powerstr = "#"; powerstr = powerstr + getCheckedNodeValue(TreeView1.Nodes); Label1.Text = powerstr; } public string getCheckedNodeValue(TreeNodeCollection aNodes) { string powerstr = ""; for (int i = 0; i < aNodes.Count; i++) { if (aNodes[i].Checked == true) { powerstr = powerstr + aNodes[i].Value + "#"; } if (aNodes[i].ChildNodes.Count > 0) { powerstr = powerstr + getCheckedNodeValue(aNodes[i].ChildNodes); } } return powerstr; }
dataset后面是自己写的方法 从数据库里绑定的。
因为要做这个功能,结合了前辈写的代码贴一下。