欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net treeview checkbox 相关问题

程序员文章站 2024-03-11 16:10:31
private void display() { dt = new tsorganize().organizeds(); dataview dv = new datavie...
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为全部层)