当前位置: 代码迷 >> ASP.NET >> 关于树形 取值解决思路
  详细解决方案

关于树形 取值解决思路

热度:3868   发布时间:2013-02-25 00:00:00.0
关于树形 取值
资源模块
  基本管理
  特别管理
  ...
时间模块
  信息管理
  正常管理
出入模块
  出入统计
  出入报表
  ....

有这么一个树 而且每个节点都有复选框 我想怎么循环得到 资源模块 时间模块 出入模块 然后根据对应的模块 去找对应的子节点 然后把此得到的结果保存到数据库 。
 也就是 先获取资源模块 这个节点 然后根据这个节点 去循环 得到子节点的选中的 值 然后把此作为一条数据保存,然后在根据时间模块 这个节点 去循环 子节点 并得到子节点选中的值 依次类推下去 ......请教大家 谢谢了

------解决方案--------------------------------------------------------
参考:
JScript code
$(document).ready(function() {        var array = new Array();        $("#sl").children().each(function() {            $(this).children().each(function() {                //判断checkbox选中 得到数据 可以push到数组array            });        });    });//#sl为资源模块 等一级模块的父级ID
------解决方案--------------------------------------------------------
供参考!我上星期写的一个树形结构数据绑定DropDownList
C# code
 private void BindDrpClass() {            IList<Cargos.CargoCategory> car = Global.CargoCategoryManager.Get();            DataTable dt = new DataTable();            dt.Columns.Add("Id", typeof(int));            dt.Columns.Add("Name", typeof(string));            dt.Columns.Add("Parent", typeof(int));            dt.Columns.Add("Description", typeof(string));            foreach (Cargos.CargoCategory c in car) {                dt.Rows.Add(new object[] { c.Id, c.Name,                    c.Parent==null?0:c.Parent.Id, c.Description });            }            DropDownList1.Items.Clear();            //DropDownList1.Items.Add(new ListItem("作为根", "0"));            //DataRow[] drs = dt.Select("Parent= "+dt.Rows[0][2]);            for (int i = 0; i < dt.Rows.Count; i++) {                if (Convert.ToInt32(dt.Rows[i][2]) == 0) {                    string classid = dt.Rows[i][0].ToString();                    string classname = dt.Rows[i][1].ToString();                    //顶级分类显示形式                    classname = "├" + classname;                    DropDownList1.Items.Add(new ListItem(classname, classid));                    int sonparentid = int.Parse(classid);                    string blank = "    ├──";                    //递归子分类方法                    BindDrpNode(sonparentid, dt, blank);                }                DropDownList1.DataBind();            }        }        private void BindDrpNode(int parentid, DataTable dt, string blank) {            for (int i = 0; i < dt.Rows.Count; i++) {                if (Convert.ToInt32(dt.Rows[i][2]) == parentid) {                    string classid = dt.Rows[i][0].ToString();                    string classname = dt.Rows[i][1].ToString();                    classname = blank + classname;                    DropDownList1.Items.Add(new ListItem(classname, classid));                    int sonparentid = int.Parse(classid);                    string blank2 = "        " + blank + "──";                    BindDrpNode(sonparentid, dt, blank2);                }            }        }
------解决方案--------------------------------------------------------
三层循环:
StringBuilder test = new StringBuilder();
foreach (TreeNode node in TreeView1.Nodes)
{
if(!node.Selected) continue;
test.Append(node.Text);
foreach (TreeNode SecondNode in node.ChildNodes)
{
if(!SecondNode.Selected) continue;
test.Append(SecondNode.Text);
foreach (TreeNode ThridNode in node.ChildNodes)
{
if(!ThridNode.Selected) continue;
test.Append(ThridNode.Text);
}
}
//这里操作你的数据保存方法。
//test.ToString();就是你要的数据。
}
递归:
private string reText(TreeNodeCollection nodes)
{
StringBuilder test = new StringBuilder();
foreach (TreeNode node in nodes)
{
if(!node.Selected) continue;
  相关解决方案