程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net treeview checkbox 相關問題

asp.net treeview checkbox 相關問題

編輯:ASP.NET基礎
private void Display()
{
dt = new TsOrganize().OrganizeDs();
DataView dv = new DataView(dt);
TreeNode nodes = new TreeNode();
nodes.Text = "兩岸咖啡";
nodes.Value = "0";
if (dt.Rows.Count >= 1)
{
dv.RowFilter = "fParentId=''";
if (dv.Count >= 1)
{
foreach (DataRowView drrow in dv)
{
TreeNode nodel = new TreeNode();
nodel.Text = drrow.Row["fOrgName"].ToString();
nodel.Value = drrow.Row["forgNo"].ToString();
nodes.ChildNodes.Add(nodel);
NodeList(dv, nodel, drrow.Row["fOrgNo"].ToString());
}
}
}
treevList.Nodes.Add(nodes);
}

//生成樹
private void NodeList(DataView dv, TreeNode node, string ParentId)
{
dv.RowFilter = "fParentId='" + ParentId.ToString() + "'";
if (dv.Count >= 1)
{
foreach (DataRowView drrow in dv)
{
TreeNode nodel = new TreeNode();
nodel.Text = drrow.Row["fOrgName"].ToString();
nodel.Value = drrow.Row["forgNo"].ToString();
node.ChildNodes.Add(nodel);
NodeList(dv, nodel, drrow.Row["fOrgNo"].ToString());
}
}
}
============================================================
1.
TreeNodeCollection tnc = treevList.CheckedNodes;
//獲取樹中所有選定的節點
private void GetCheckNode(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
if(node.Checked)
{
if (node.ChildNodes.Count == 0)
{
DataRow dr = tblSelChains.NewRow();
dr["forgNo"] = node.Value;
dr["fOrgName"] = node.Text.ToString();
tblSelChains.Rows.Add(dr);
}
}
}
}

2.根據根據數據庫中的值,給相應的節點的CHECKED=true

/// <summary>
///dtPloyStore :是表(保存從數據庫中讀取的數據)
/// <para></para>
/// </summary>
TreeNodeCollection tnc = treevList.Nodes ;
if (dtPloyStore != null)
{
if (dtPloyStore.Rows.Count > 0)
{
for (int i = 0; i < dtPloyStore.Rows.Count; i++)
{
GetSelectedTreeNode(tnc, dtPloyStore.Rows[i]["fOrgNo"].ToString());
}
}
}
/// <summary>
/// 遍歷門店樹,得到選中的節點
/// <para></para>
/// </summary>
/// <param name="tnc"></param>
public void GetSelectedTreeNode(TreeNodeCollection tnc,string CheckData)
{
foreach (TreeNode node in tnc)
{
if (node.Checked)
{
node.Checked = true;
break;
}
GetSelectedTreeNode(node.ChildNodes, CheckData);
}
}

3.javascript實現選中父節點,子節點自動選中
///後台給treeview添加屬性
/// this.treevList.Attributes.Add("onclick", "OnTreeNodeChecked()");

///下面是在前台javascript中編寫的
function OnTreeNodeChecked() {
var ele = window.event.srcElement;
if (ele.type == 'checkbox') {
var childrenDivID = ele.id.replace('CheckBox', 'Nodes');
var div = document.getElementById(childrenDivID);
if (div == null) return;
var checkBoxs = div.getElementsByTagName('INPUT');
for (var i = 0; i < checkBoxs.length; i++) {
if (checkBoxs[i].type == 'checkbox')
checkBoxs[i].checked = ele.checked;
}
}
4.在 treeview上顯示CHECKBOX,其實很簡單,只要設置 ShowCheckBoxes="All"
如果我們想顯示treeviw的設定層,我們可以設置 ExpandDepth="N" (N為數字,就是你想顯示的層數,0為全部層)
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved