.net的卖点是控件多,要掌握这些控件,熟练操控可不是容易的事,看MSDN,看参考书而不投入实际,永远不能活学活用的。故做demo是最好的事情,这次介绍treeview控件,这里说的是ASP.net的。用法其实和winfrom的也相仿。
先看代码
Code
Code
public partial class _Default : System.Web.UI.Page
{
WebStoreDataContext db = new WebStoreDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
int ?id = Convert.ToInt32(Request.QueryString["id"]);
TreeNode nodes = new TreeNode();
//因为是树结构,所以实例化一个树节点
nodes.Text = "商品分类";//为节点加名字
nodes.NavigateUrl = "Default.aspx";
//加连接
TreeView1.Nodes.Add(nodes);//把东西加进treeview
var list = from c in db.Categories select c;//这句是从数据库查数据
foreach (Category cate in list)
{ //创建子节点
TreeNode node = new TreeNode();
node.Text = cate.CategoryName;
node.NavigateUrl = string.Format("Default.aspx?id={0}",cate.CategoryId);
//在第一个父节点,加上子节点
TreeView1.Nodes[0].ChildNodes.Add(node);
}
BindAll(id);
}
}
private void BindAll(int? id)
{
if (id >0)
{
var lists = from c in db.Products
where c.CategoryId == id
select c;
DataList1.DataSource = lists;
}
else if(id==0 || id==null){
var lists = from c in db.Products
select c;
DataList1.DataSource = lists;
}
DataList1.DataBind();
}
Code
public partial class _Default : System.Web.UI.Page
{
WebStoreDataContext db = new WebStoreDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
int ?id = Convert.ToInt32(Request.QueryString["id"]);
TreeNode nodes = new TreeNode();
//因为是树结构,所以实例化一个树节点
nodes.Text = "商品分类";//为节点加名字
nodes.NavigateUrl = "Default.aspx";
//加连接
TreeView1.Nodes.Add(nodes);//把东西加进treeview
var list = from c in db.Categories select c;//这句是从数据库查数据
foreach (Category cate in list)
{ //创建子节点
TreeNode node = new TreeNode();
node.Text = cate.CategoryName;
node.NavigateUrl = string.Format("Default.aspx?id={0}",cate.CategoryId);
//在第一个父节点,加上子节点
TreeView1.Nodes[0].ChildNodes.Add(node);
}
BindAll(id);
}
}
private void BindAll(int? id)
{
if (id >0)
{
var lists = from c in db.Products
where c.CategoryId == id
select c;
DataList1.DataSource = lists;
}
else if(id==0 || id==null){
var lists = from c in db.Products
select c;
DataList1.DataSource = lists;
}
DataList1.DataBind();
}
还有很多用法,这段代码是关于treeview如何在后台绑定数据的。还有其他以后再补充