首先在 PageLoad写如下方法:
//为了点击TreeView复选框触发服务器事件 this.tvDepartment.Attributes.Add("onclick", "postBackByObject()");
在ASPX页面端,写如下js代码:
<script type="text/javascript"> // 点击TreeView复选框时触发事件 function postBackByObject() { var o = window.event.srcElement; if (o.tagName == "INPUT" && o.type == "checkbox") { __doPostBack("UpdatePanel1", ""); } } </script>
需要在树控件上,写上事件触发,如下:
<powerProject:TreeViewDepartments ID="tvDepartment" runat="server" Scope="All" ShowCheckBoxes="All" OnTreeNodeCheckChanged="tvDepartment_TreeNodeCheckChanged"></powerProject:TreeViewDepartments>
在C#代码中,写触发事件代码:
/// <summary> /// 选中部门节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void tvDepartment_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e) { //.... }
如果需要,可以把子孙节点都设置选中:
/// <summary> /// 选中父节点,选中全部子节点,并且添加和类别之间的关联 /// </summary> /// <param name="parentNode"></param> private void SetChildNodeChecked(TreeNode parentNode) { foreach (TreeNode childNode in parentNode.ChildNodes) { childNode.Checked = true; this.SetChildNodeChecked(childNode); // do something.... } }