一 :
效果
<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);
}
}
}
二 :
点击 “水果类”
<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;
}
}