TreeView 用法(有代碼)(asp.net)_javascript技巧

來源:互聯網
上載者:User
前台代碼
複製代碼 代碼如下:

<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());
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.