【问题标题】:I want to use an asp Treeview control with LinqToSql我想使用带有 LinqToSql 的 asp Treeview 控件
【发布时间】:2014-02-10 12:02:27
【问题描述】:

我正在尝试了解如何实现树视图控件 - 这一切看起来都非常复杂。但是,Treeview 控件会更合适。

我有一个包含字段 ID 和 ParentLevelID 的 SQL 表。

我在代码中添加了一个基本的 Treeview 控件:

        <asp:TreeView ID="tvLevels" runat="server">
        </asp:TreeView>

我想使用 LinqToSQL 填充此表。目前,我正在显示与 Gridview 相同的数据:

    protected void SetupLevelsPanel()
    {
        // display levels according to current parentId
        _svsCentralDataContext = new SVSCentralDataContext();
        object levels;
        if (_intParentLevelId == 0)
        {
            levels = (from sl in _svsCentralDataContext.SVSSurvey_Levels
            where sl.ParentLevelID == null && sl.SurveyID == _intSurveyId
            select new
            {
                sl.ID,
                sl.SurveyID,
                sl.UserCode,
                sl.ExternalRef,
                sl.Description,
                sl.ParentLevelID,
                sl.LevelSequence,
                sl.Active
            });
            backUpButton.Visible = false;
        }
        else
        {
            levels = (from sl in _svsCentralDataContext.SVSSurvey_Levels
            where sl.ParentLevelID == _intParentLevelId && sl.SurveyID == _intSurveyId
            select new
            {
                sl.ID,
                sl.SurveyID,
                sl.UserCode,
                sl.ExternalRef,
                sl.Description,
                sl.ParentLevelID,
                sl.LevelSequence,
                sl.Active
            });
        }


        grdLevels.DataSource = levels;
        grdLevels.DataBind();
        GrdLevelButtons();
    }

如何转换这些信息以使用我的 Treeview 控件?

【问题讨论】:

  • 无需回复。我已经破解了。
  • 嗨,史蒂夫。很高兴你得到你的答案 =) 仅供参考 - answer your own question 没关系;甚至鼓励!随时发布您的解决方案作为答案,以更清楚地表明您的问题已得到解决。

标签: c# asp.net visual-studio-2010 treeview


【解决方案1】:

这是我的解决方案。 在我的后台代码页面上:

    private void BuildTree()
    {
        tvLevels .Nodes.Clear();
        _svsCentralDataContext = new SVSCentralDataContext();
        List<DataAccessLayer.Level> items = DataAccessLayer.Levels.GetLevels(_intSurveyId).ToList();

        List<DataAccessLayer.Level> rootItems = items.FindAll(p => p.ParentLevelId == null);

        foreach (DataAccessLayer.Level item in rootItems)
        {
            var tvi = new TreeNode(item.Description, item.Id.ToString(CultureInfo.InvariantCulture) );
            BuildChildNodes(tvi, items, item.Id);
            tvLevels.Nodes.Add(tvi);
        }
    }

    private void BuildChildNodes(TreeNode parentNode, List<DataAccessLayer.Level> items, int parentId)
    {
        List<DataAccessLayer.Level> children = items.FindAll(p => p.ParentLevelId == parentId).ToList();
        foreach (DataAccessLayer.Level item in children)
        {
            var tvi = new TreeNode(item.Description, item.Id.ToString(CultureInfo.InvariantCulture));
            parentNode.ChildNodes.Add(tvi);
            BuildChildNodes(tvi, items, item.Id);
        }
    }

Class Levels.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using SVSVoidSurveyDesigner.Database;

   namespace SVSVoidSurveyDesigner.DataAccessLayer
   {
   public class Levels
   {
    public static IEnumerable<Level> GetLevels(int intSurveyId)
    {
        var dataContext = new SVSCentralDataContext();


        var levels = (from l in dataContext.SVSSurvey_Levels where l.SurveyID == intSurveyId
                             select new Level
                             {
                                 Id = l.ID,
                                 SurveyId = l.SurveyID,
                                 UserCode = l.UserCode ,
                                 ExternalRef = l.ExternalRef ,
                                 Description = l.Description ,
                                 ParentLevelId = (l.ParentLevelID),
                                 LevelSequence = ( l.LevelSequence ),

                                 Active = Convert .ToBoolean( l.Active )
                             });

        return levels;
    }
}

}

类级别.cs

    namespace SVSVoidSurveyDesigner.DataAccessLayer
    {
        public class Level
        {
        public int Id { get; set; }
        public int SurveyId { get; set; }
        public string UserCode { get; set; }
        public string ExternalRef { get; set; }
        public string Description { get; set; }
        public int? ParentLevelId { get; set; }
        public int? LevelSequence { get; set; }
        public bool Active { get; set; }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多