TreeViewの使い方(コードあり)(asp.net)
3348 ワード
フロントコード
バックグラウンドコード:
<br>//
<br>function CheckEvent() {
<br>var objNode = event.srcElement;
<br>if (objNode.tagName == "INPUT" && objNode.type == "checkbox") {
<br>var objParentDiv = objNode.id.replace("CheckBox", "Nodes");
<br>if (objNode.checked == true) {
<br>setChildCheckState(objParentDiv, true);
<br>setParentCheckeState(objNode, true);
<br>}
<br>else {
<br>setChildCheckState(objParentDiv, false);
<br>if (!HasOtherChecked(objNode)) {
<br>setParentCheckeState(objNode, false);
<br>}
<br>}
<br>}
<br>}
<br>//
<br>function HasOtherChecked(objNode) {
<br>var objParentDiv = WebForm_GetParentByTagName(objNode, "div");
<br>var chks = objParentDiv.getElementsByTagName("INPUT");
<br>for (var i = 0; i < chks.length; i++) {
<br>if (chks[i].checked && chks[i].id != objNode.id) {
<br>return true;
<br>}
<br>}
<br>return false;
<br>}
<br>//
<br>function setParentCheckeState(objNode, chkstate) {
<br>try {
<br>var objParentDiv = WebForm_GetParentByTagName(objNode, "div");
<br>if (objParentDiv == null || objParentDiv == "undefined ") {
<br>return;
<br>}
<br>else {
<br>var objParentChkId = objParentDiv.id.replace("Nodes", "CheckBox");
<br>var objParentCheckBox = document.getElementById(objParentChkId);
<br>if (objParentCheckBox) {
<br>objParentCheckBox.checked = chkstate;
<br>setParentCheckeState(objParentDiv, chkstate);
<br>}
<br>}
<br>}
<br>catch (e) { }
<br>}
<br>//
<br>function setChildCheckState(nodeid, chkstate) {
<br>var node = document.getElementById(nodeid);
<br>if (node) {
<br>var chks = node.getElementsByTagName("INPUT");
<br>for (var i = 0; i < chks.length; i++) {
<br>chks[i].checked = chkstate;
<br>}
<br>}
<br>}
<br>
バックグラウンドコード:
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());
}
}