TreeView 控件 Checkbox 级联选择
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!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></title> <script type="text/javascript"> var TreeViewControl; /*var TreeVieMultipleSelect=true;*/ var TreeInitialize = false; function CheckChildNodes(checkBox) { var checked = checkBox.checked; var layer = document.getElementById(checkBox.id.substring(0, checkBox.id.lastIndexOf("CheckBox")) + "Nodes"); if (layer) SetControlsChecked(layer.children, checked); SetParentNodeChecked(checkBox); } function SetParentNodeChecked(checkBox) { var regExp = new RegExp(TreeViewControl.id + "n[\\d]*Nodes"); if (checkBox.checked) { SetParentCheckBoxChecked(checkBox); return; } var cellIndex = checkBox.parentNode.cellIndex; for (var layer = checkBox; layer.id != TreeViewControl.id; layer = layer.parentNode) { if (!regExp.test(layer.id)) continue; for (var i = 0; i < layer.children.length; i++) { var table = layer.children[i]; if (table.nodeName.toLowerCase() != "table") continue; if (table.rows[0].cells[cellIndex].children[0].checked) return; } checkBox = document.getElementById(layer.id.substring(0, layer.id.lastIndexOf("Nodes")) + "CheckBox"); checkBox.checked = false; cellIndex = checkBox.parentNode.cellIndex; } } function SetParentCheckBoxChecked(checkBox) { var regExp = new RegExp(TreeViewControl.id + "n[\\d]*Nodes"); for (var layer = checkBox.parentNode; layer.id != TreeViewControl.id; layer = layer.parentNode) { if (!regExp.test(layer.id)) continue; parentCheckBox = document.getElementById(layer.id.substring(0, layer.id.lastIndexOf("Nodes")) + "CheckBox"); parentCheckBox.checked = checkBox.checked; } } function SetControlsChecked(controls, checked) { for (var i = 0; i < controls.length; i++) { controls[i].checked = checked; SetControlsChecked(controls[i].children, checked); } } function CheckTree(tree) { TreeViewControl = tree; if (tree.MultipleSelect == null) tree.MultipleSelect = true; else tree.MultipleSelect = eval(tree.MultipleSelect.toString().toLocaleLowerCase()); if (!TreeInitialize) { InitializeTree(tree, tree.children); TreeInitialize = true; } CheckNode(tree); } function CheckNode(checkBox/*tree*/) { if (TreeViewControl.MultipleSelect) { CheckChildNodes(checkBox); return; } ClearSelection(TreeViewControl.children, checkBox.id); } function ClearSelection(controls, checkBoxID) { for (var i = 0; i < controls.length; i++) { if (controls[i].id != checkBoxID) controls[i].checked = false; ClearSelection(controls[i].children, checkBoxID); } } function InitializeTree(tree, controls) { var regExp = new RegExp(tree.id + "n[\\d]*CheckBox"); for (var i = 0; i < controls.length; i++) { var checkBox = controls[i]; if (regExp.test(checkBox.id)) checkBox.onclick = function () { CheckNode(this) }; InitializeTree(tree, checkBox.children); } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" OnClick="CheckTree(this)"> </asp:TreeView> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { for (int i = 0; i < 5; i++) { TreeNode masterNode = new TreeNode(i.ToString()); TreeView1.Nodes.Add(masterNode); for (int j = 0; j < 5; j++) { TreeNode childNode = new TreeNode(j.ToString()); masterNode.Expanded = false; masterNode.ChildNodes.Add(childNode); } } } } } }
private string FilterByDeptCode(string ids, string codes)
{
List<string> codeList = codes.Split(',').ToList();
List<string> idList = ids.Split(',').ToList();
for (int i = 0; i < codeList.Count - 1; i++)
{
for (int y = idList.Count - 1; y > i; y--)
{
if (codeList[y].StartsWith(codeList[i]))
{
idList.RemoveAt(y);
}
}
}
if (idList.Count > 0)
return string.Join(",", idList);
return string.Empty;
}
select ' or DepartmentCode like ''' + cast(DepartmentCode as varchar) + '%''' from TBasCorpRight inner join TBasDepartment on TBasDepartment.DepartmentID=TBasCorpRight.DepartmentID