.net 2005使用treeview,要求绑定中国和各个省市。操作系统是2003 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先取出第一级目录数据,即所有的省级行政单位,循环绑定到treenode然后根据父节点的value取出下面市的数据,绑定到该节点下,两个方法就搞定了treeview的绑定实在是个简单东西,连这个都不愿意自己去钻研和尝试一下,还怎么学好ASP.NET>? 批评得很好。我是不能显示treeview控件,显示的是文本,请留意是.net 2005. .net 2005自带了一个treeview(System.Web.UI.WebControls)但是没有GetNodeFromIndex方法。用Microsoft.Web.UI.WebControls下的显示不正常,我装了IEWebControls.exe 那更要挨批了,.NET2005下的treeview方法都差不多,更容易循环指定了每个treenode节点的Text属性就能显示出来了等你实现了我想你应该会感到羞愧的..看看这个帖子把 http://topic.csdn.net/u/20080504/14/50dd2578-0850-4f68-8053-a6ab79f26351.html?seed=902170456我在另外一个帖子有回答,中间有搂主贴的绑定代码 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TreeNode nd = new TreeNode(); nd.Value = "0001"; //存放节点 ID nd.Text = "中国"; //设置节点名称 TreeView1.Nodes.Add(nd); //添加子节点 this.PopulateNodes(TreeView1.Nodes[0].ChildNodes,"0001"); } } private void PopulateNodes(System.Web.UI.WebControls.TreeNodeCollection theNode,string parentID) { DataTable dt = this.getDataTable(); DataRow[] rows = dt.Select("ParentGovernmentID=" + parentID); //筛选出属于父节点 “node”的子节点集合 foreach (DataRow dr in rows) //循环子节点集合 { TreeNode nd = new TreeNode(); nd.Value = dr["GovernmentID"].ToString(); //存放节点 ID nd.Text = dr["GovernmentName"].ToString(); //设置节点名称 this.AddNodes(dt, nd, nd.Value); //递归,添加该节点的子节点 //if (theNode == null || parentID == "0001") if (theNode == null ) { this.TreeView1.Nodes.Add(nd); //添加至根节点 } else { theNode.Add(nd); //添加子节点 } } } private void AddNodes(DataTable dt, TreeNode node, string id) { DataRow[] rows = dt.Select("ParentGovernmentID=" + id); //筛选出属于父节点 “node”的子节点集合 foreach (DataRow dr in rows) //循环子节点集合 { TreeNode nd = new TreeNode(); //nd.NavigateUrl = dr["URL"].ToString(); //设置浏览的网址 nd.Value = dr["GovernmentID"].ToString(); //存放节点 ID nd.Text = dr["GovernmentName"].ToString(); //设置节点名称 this.AddNodes(dt, nd, nd.Value); //递归,添加该节点的子节点 //if (node == null || id == "0001") if (node == null) { this.TreeView1.Nodes.Add(nd); //添加至根节点 } else { node.ChildNodes.Add(nd); //添加子节点 } } } private DataTable getDataTable() { DbConn dbconn = new DbConn(); string ConnectionString = dbconn.GetConnPara(); this.Hid_ConnectionString.Value = ConnectionString; SqlConnection conn = new SqlConnection(ConnectionString); SqlDataAdapter MyAdapter = new SqlDataAdapter(); SqlCommand comm = new SqlCommand(); //选择DMSupplier中的记录 comm.CommandText = "select GovernmentID,ParentGovernmentID,GovernmentName from TblGovernment where GovernmentID != '0001' and GovernmentID != '01' order by GovernmentID"; //comm.CommandText = "select GovernmentID,ParentGovernmentID,GovernmentName from TblGovernment where GovernmentID != '0001' order by GovernmentID"; comm.Connection = conn; MyAdapter.SelectCommand = comm; DataSet ds = new DataSet(); MyAdapter.Fill(ds, "TblGovernment"); return ds.Tables["TblGovernment"]; }表结构GovernmentID nvarchar 100GovernmentName nvarchar 100ParentGovernmentID nvarchar 100为什么只邦定了北京及其下属区域,其他省份的只邦定了省份。请高人回答 。 GovernmentID GovernmentName ParentGovernmentID0001 中国 000011 北京 00011101 北京市辖 11110101 海淀 1101...12 天津 0001 怎么将vs2005自带的精简版数据库中的表定义转出到word中? Membership web.config权限配置问题,总是登录不进去。 关于服务器播放flash 动软代码生成器的缓存模式是怎么实现的? 把Excel导入到GridView里 ————请教关于XSL,谢谢 请问如何在DataGrid控件中显示表格时,如何让每列的宽度固定 如何定义类和方法 TreeView怎么绑定动态生成的XML文件? WEB 下 DataGrid的 DataKeyField属性有什么用啊? 在线等-导出的Excel格式问题 点击treeview节点的的导航问题?
那更要挨批了,.NET2005下的treeview方法都差不多,更容易循环指定了每个treenode节点的Text属性就能显示出来了等你实现了我想你应该会感到羞愧的..看看这个帖子把 http://topic.csdn.net/u/20080504/14/50dd2578-0850-4f68-8053-a6ab79f26351.html?seed=902170456我在另外一个帖子有回答,中间有搂主贴的绑定代码
{
if (!IsPostBack)
{
TreeNode nd = new TreeNode();
nd.Value = "0001"; //存放节点 ID
nd.Text = "中国"; //设置节点名称
TreeView1.Nodes.Add(nd); //添加子节点 this.PopulateNodes(TreeView1.Nodes[0].ChildNodes,"0001");
}
} private void PopulateNodes(System.Web.UI.WebControls.TreeNodeCollection theNode,string parentID)
{
DataTable dt = this.getDataTable();
DataRow[] rows = dt.Select("ParentGovernmentID=" + parentID); //筛选出属于父节点 “node”的子节点集合
foreach (DataRow dr in rows) //循环子节点集合
{
TreeNode nd = new TreeNode();
nd.Value = dr["GovernmentID"].ToString(); //存放节点 ID
nd.Text = dr["GovernmentName"].ToString(); //设置节点名称
this.AddNodes(dt, nd, nd.Value); //递归,添加该节点的子节点 //if (theNode == null || parentID == "0001")
if (theNode == null )
{
this.TreeView1.Nodes.Add(nd); //添加至根节点
}
else
{
theNode.Add(nd); //添加子节点
}
}
} private void AddNodes(DataTable dt, TreeNode node, string id)
{
DataRow[] rows = dt.Select("ParentGovernmentID=" + id); //筛选出属于父节点 “node”的子节点集合
foreach (DataRow dr in rows) //循环子节点集合
{
TreeNode nd = new TreeNode();
//nd.NavigateUrl = dr["URL"].ToString(); //设置浏览的网址
nd.Value = dr["GovernmentID"].ToString(); //存放节点 ID
nd.Text = dr["GovernmentName"].ToString(); //设置节点名称
this.AddNodes(dt, nd, nd.Value); //递归,添加该节点的子节点 //if (node == null || id == "0001")
if (node == null)
{
this.TreeView1.Nodes.Add(nd); //添加至根节点
}
else
{
node.ChildNodes.Add(nd); //添加子节点
}
}
} private DataTable getDataTable()
{
DbConn dbconn = new DbConn();
string ConnectionString = dbconn.GetConnPara();
this.Hid_ConnectionString.Value = ConnectionString;
SqlConnection conn = new SqlConnection(ConnectionString);
SqlDataAdapter MyAdapter = new SqlDataAdapter();
SqlCommand comm = new SqlCommand(); //选择DMSupplier中的记录
comm.CommandText = "select GovernmentID,ParentGovernmentID,GovernmentName from TblGovernment where GovernmentID != '0001' and GovernmentID != '01' order by GovernmentID";
//comm.CommandText = "select GovernmentID,ParentGovernmentID,GovernmentName from TblGovernment where GovernmentID != '0001' order by GovernmentID";
comm.Connection = conn;
MyAdapter.SelectCommand = comm; DataSet ds = new DataSet();
MyAdapter.Fill(ds, "TblGovernment");
return ds.Tables["TblGovernment"];
}
表结构GovernmentID nvarchar 100
GovernmentName nvarchar 100
ParentGovernmentID nvarchar 100
为什么只邦定了北京及其下属区域,其他省份的只邦定了省份。请高人回答 。
0001 中国 0000
11 北京 0001
1101 北京市辖 11
110101 海淀 1101
...12 天津 0001