asp.net treeview checkbox 相關問題

來源:互聯網
上載者:User

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為全部層)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.