TreeView 用法(有代码)(asp.net)

时间:2021-05-26

前台代码
复制代码 代码如下:
<script type="text/javascript">
//客户端捕捉事件
function CheckEvent() {
var objNode = event.srcElement;
if (objNode.tagName == "INPUT" && objNode.type == "checkbox") {
var objParentDiv = objNode.id.replace("CheckBox", "Nodes");
if (objNode.checked == true) {
setChildCheckState(objParentDiv, true);
setParentCheckeState(objNode, true);
}
else {
setChildCheckState(objParentDiv, false);
if (!HasOtherChecked(objNode)) {
setParentCheckeState(objNode, false);
}
}
}
}
//判断是否有并行的其他节点被选中
function HasOtherChecked(objNode) {
var objParentDiv = WebForm_GetParentByTagName(objNode, "div");
var chks = objParentDiv.getElementsByTagName("INPUT");
for (var i = 0; i < chks.length; i++) {
if (chks[i].checked && chks[i].id != objNode.id) {
return true;
}
}
return false;
}
//设置父节点
function setParentCheckeState(objNode, chkstate) {
try {
var objParentDiv = WebForm_GetParentByTagName(objNode, "div");
if (objParentDiv == null || objParentDiv == "undefined ") {
return;
}
else {
var objParentChkId = objParentDiv.id.replace("Nodes", "CheckBox");
var objParentCheckBox = document.getElementById(objParentChkId);
if (objParentCheckBox) {
objParentCheckBox.checked = chkstate;
setParentCheckeState(objParentDiv, chkstate);
}
}
}
catch (e) { }
}
//设置子节点
function setChildCheckState(nodeid, chkstate) {
var node = document.getElementById(nodeid);
if (node) {
var chks = node.getElementsByTagName("INPUT");
for (var i = 0; i < chks.length; i++) {
chks[i].checked = chkstate;
}
}
}
</script>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" onclick="CheckEvent()">
</asp:TreeView>
</form>
</body>

后台代码:
复制代码 代码如下:
private void BindGrid()
{
DataTable mytable = menuTableBll.GetAllList().Tables[0]; //获取数据
CreateTree(mytable, TreeView1.Nodes, "0");
}
private void CreateTree(DataTable dtNodeSets, TreeNodeCollection node, string parent_id)
{
DataView dvList = new DataView(dtNodeSets);
dvList.RowFilter = "MeunParentId =" + parent_id;
TreeNode nodeTemp;
foreach (DataRowView dv in dvList)
{
nodeTemp = new TreeNode();
nodeTemp.Text = dv["MenuName"].ToString();
nodeTemp.Expanded = true;
nodeTemp.ToolTip = dv["MeunId"].ToString();
node.Add(nodeTemp);
CreateTree(dtNodeSets, nodeTemp.ChildNodes, dv["MeunId"].ToString());
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章