使用嵌套字典填充树视图

时间:2014-03-22 12:42:49

标签: c# asp.net dictionary treeview

我有一个像这样的管理表

ID    Name           Parent
1   Manager             0
2   Accountant          0
3   Assistant Manager   1
4   Branch Manager      1
5   Employee1           3
6   Employee3           3
7   Employee2           4
8   Accountant1         2
9   Accountant2         2

父代表我的asp页面中的树视图结构

*Manager-->Assistant manager-->Employee1
        |                   |->Employee3
        |
        |-->Branch Manager---->Employee2
*Accountant-->Accountant1
           |->Accountant2    

我只需要使用字典来显示我的数据表到树视图 所以我在下面的代码中将数据表绑定到字典。 我需要从上面的嵌套字典中获取字符串值。怎么做。

management = dt.AsEnumerable()
               .GroupBy(m => m.Field<int>("ID"))
               .ToDictionary(
                   a => a.Key,
                   a => a.GroupBy(c => c.Field<string>("Name"))
                         .ToDictionary(
                             d => d.Key,
                             d => d.First().Field<int>("Parent")));

现在我正在尝试将字典绑定到treeview 在我的代码背后,我做到了这一点 public partial class TreeView:System.Web.UI.Page {     protected void Page_Load(object sender,EventArgs e)     {         if(!IsPostBack)         {             DisplayTree();         }     }

public void DisplayTree()
{
    try
    {
        TreeNode parentnode = new TreeNode();
        ManagementBL l_bl_manag = new ManagementBL();
        Dictionary<int, Dictionary<string, int>> manage = new Dictionary<int, Dictionary<string, int>>();
        manage = l_bl_manag.Adddictionary();
        foreach (var kvp in manage)
        {
            var innerdict = kvp.Value;
            foreach (var innerkvp in innerdict)
            {
                parentnode = new TreeNode(innerkvp.Key);
                parentnode.Text = innerkvp.Key; 
                if (kvp.Key == innerkvp.Value)
                {
                    parentnode.ChildNodes.Add(new TreeNode(innerkvp.Key));
                }
            }
            tv_Management.Nodes.Add(parentnode);
        }
        this.Controls.Add(tv_Management);
    }

    catch (Exception ex)
    {
        string error = ex.Message.ToString();
        lb_msg.Text = "Unknown error on this page. Please try again later";
    }
}

请帮助我收到以下错误。

Control 'tv_Management' of type 'TreeView' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Control 'tv_Management' of type 'TreeView' must be placed inside a form tag with runat=server.

我也知道我的代码隐藏尚未完成。请帮忙 PS:我是asp的新手

伙计们还没有帮助:(

0 个答案:

没有答案