码迷,mamicode.com
首页 > 其他好文 > 详细

绑定 treeview树

时间:2014-10-15 18:34:51      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   os   ar   java   for   sp   

 

 一 : 

效果

bubuko.com,布布扣

<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" runat="server" Height="300px" Width="200px" SelectAction="Expand">
</asp:TreeView>
</form>

 

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string where = " AND Com_IsTrue=0 AND Com_Status=2 ";
BDTree(where);
}
}

private List<T_CommodityType> list = new List<T_CommodityType>();
private void BDTree( string where )
{
list = bll.GetModelList(where );
AddTree("", null);
}

public void AddTree(string ParentID, TreeNode pNode)
{
if (list.Where(o => o.Com_ParentId.ToString().Equals(ParentID)).Count() == 0)
return;
List<T_CommodityType> clist = list.Where(o => o.Com_ParentId.ToString().Equals(ParentID)).ToList();
foreach (T_CommodityType dc in clist)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
Node.Value = dc.Com_Id.ToString();
Node.Text = dc.Com_Name;
Node.NavigateUrl = "javascript:SaveSelData(‘" + Node.Text + "‘,‘" + Node.Value + "‘);";
TreeView1.Nodes.Add(Node);
AddTree(dc.Com_Id.ToString(), Node);
}
else
{
Node.Value = dc.Com_Id.ToString();
Node.Text = dc.Com_Name;
Node.NavigateUrl = "javascript:SaveSelData(‘" + Node.Text + "‘,‘" + Node.Value + "‘);";
pNode.ChildNodes.Add(Node);
AddTree(dc.Com_Id.ToString(), Node);
}
}
}

 二 :

bubuko.com,布布扣

 

点击 “水果类”

bubuko.com,布布扣

 

<div>
<asp:TreeView ID="TreeView1" runat="server" Height="300px" Width="200px" SelectAction="Expand"
ShowCheckBoxes="All" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" OnLoad="TreeView1_Load">
</asp:TreeView>
</div>

 

 

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BDTree();
}
}
private List<T_CommodityType> list = new List<T_CommodityType>();
private void BDTree()
{
list = bll.GetModelList(" AND Com_IsTrue=0 AND Com_Status=2 ; ");
AddTree("00000000-0000-0000-0000-000000000000", null);
}
public void AddTree(string ParentID, TreeNode pNode)
{
if (list.Where(o => o.Com_ParentId.ToString().Equals(ParentID)).Count() == 0)
return;
List<T_CommodityType> clist =
list.Where(o => o.Com_ParentId.ToString().ToLower().Equals(ParentID.ToLower())).ToList();
foreach (T_CommodityType dc in clist)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
Node.Value = dc.Com_Id.ToString();
Node.Text = dc.Com_Name;
Node.ShowCheckBox = true;
TreeView1.Nodes.Add(Node);
AddTree(dc.Com_Id.ToString(), Node);
}
else
{
Node.Value = dc.Com_Id.ToString();
Node.Text = dc.Com_Name;
Node.ShowCheckBox = true;
//Node.SelectAction = TreeNodeSelectAction.SelectExpand;
pNode.ChildNodes.Add(Node);
AddTree(dc.Com_Id.ToString(), Node);
}

}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
if (TreeView1.SelectedNode.Checked)
{
TreeView1.SelectedNode.Checked = false;
if (TreeView1.SelectedNode.ChildNodes.Count > 0)
{
foreach (TreeNode n in TreeView1.SelectedNode.ChildNodes)
{
n.Checked = true;
}
}
return;
}
TreeView1.SelectedNode.Checked = true;

if (TreeView1.SelectedNode.ChildNodes.Count > 0)
{
foreach (TreeNode n in TreeView1.SelectedNode.ChildNodes)
{
n.Checked = true;
}
}
if (TreeView1.SelectedNode.Parent != null)
{
// TreeView1.SelectedNode.Parent.Checked = true;
}
}

 

protected void TreeView1_Load(object sender, EventArgs e)
{
if (!object.Equals(null, TreeView1.SelectedNode))
{
TreeView1.SelectedNode.Select();
TreeView1.SelectedNode.Selected = false;
}
}

绑定 treeview树

标签:des   blog   http   io   os   ar   java   for   sp   

原文地址:http://www.cnblogs.com/lk516924/p/4026742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!