protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// PopulateTreeView();
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());
string sql = "select purview from wk_role where roleid=10";
sqlcon.Open();
SqlCommand cmd = new SqlCommand(sql, sqlcon);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
string purview = reader["purview"].ToString();
reader.Dispose();
string sqlcom = "select value from wk_menu ";
cmd = new SqlCommand(sqlcom, sqlcon);
SqlDataReader reader1 = cmd.ExecuteReader();
while (reader1.Read())
{
string value1 = reader1["value"].ToString();
//Response.Write(value1);
if (purview.Contains(value1))
{
Response.Write(value1);
PopulateTreeView(value1);
}
}
//sqlcon.Close();
}
}
public void PopulateTreeView(string value1)
{
DataTable treeViewData = GetTreeViewData(value1);
AddTopViewNodes(treeViewData);
}
private DataTable GetTreeViewData(string value1)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());
string slecteCommand = "select id,parentid,text,value,url,target from wk_menu where value='" + value1 + "' ";
SqlDataAdapter dad = new SqlDataAdapter(slecteCommand, sqlcon);
DataTable dt = new DataTable();
dad.Fill(dt);
return dt;
//sqlcon.Close();
}
private void AddTopViewNodes(DataTable treeViewData)
{
DataView view = new DataView(treeViewData);
view.RowFilter = "parentid is null";
foreach (DataRowView row in view)
{
TreeNode newnode = new TreeNode(row["text"].ToString(), row["value"].ToString());
newnode.NavigateUrl = row["url"].ToString().Trim();
newnode.Target = row["target"].ToString().Trim();
TreeView1.Nodes.Add(newnode);
AddChildTreeViewNodes(treeViewData, newnode);
}
}
private void AddChildTreeViewNodes(DataTable treeViewData, TreeNode parentTreeViewNode)
{
DataView view = new DataView(treeViewData);
// Response.Write(parentTreeViewNode.Value);
view.RowFilter = "parentid = " + parentTreeViewNode.Value;
foreach (DataRowView row in view)
{
TreeNode newnode = new TreeNode(row["text"].ToString(), row["value"].ToString());
newnode.NavigateUrl = row["url"].ToString().Trim();
newnode.Target = row["target"].ToString().Trim();
parentTreeViewNode.ChildNodes.Add(newnode);
AddChildTreeViewNodes(treeViewData, newnode);
}
}
for循环中的 AddChildTreeViewNodes(treeViewData, newnode);这个不要了,只循环一下,就是求最上层的父节点啊。
不行呢 你能否加我qq帮我远程一下 2573393999 谢谢了
参考下这里 http://www.cnblogs.com/psforever/archive/2012/05/09/2491048.html
一樓的就是答案了
private void AddTopViewNodes(DataTable treeViewData)
{
DataView view = new DataView(treeViewData);
view.RowFilter = "parentid is null";
foreach (DataRowView row in view)
{
TreeNode newnode = new TreeNode(row["text"].ToString(), row["value"].ToString());
newnode.NavigateUrl = row["url"].ToString().Trim();
newnode.Target = row["target"].ToString().Trim();
TreeView1.Nodes.Add(newnode);
// AddChildTreeViewNodes(treeViewData, newnode); //<- 這個註解掉
}
}