从服务器端展开辐射树视图

本文关键字:视图 辐射 服务器端 | 更新日期: 2023-09-27 17:56:44

我正在绑定来自DatatableTelerik RadTreeView。我想在root level (node.level=0)扩展radTreeView节点,并且它们的子节点仅从服务器端(node.level=1)

数据表包含一列,用于保存节点级别的值。但radTreeView根本没有扩大。

这是代码:

IList<RadTreeNode> allNodesBS = tvAssets.GetAllNodes();
for (int i = 0; i < allNodesBS.Count; i++)
    {
       RadTreeNode node = (RadTreeNode)allNodesBS[i];
       string nodeLevel = dt.Rows[i]["bs_node_level"].ToString();
       if (nodeLevel == "0" || nodeLevel == "1")
          {                        
             node.Expanded = true;        //doesn't work                    
          }
    }
    //tvAssets.ExpandAllNodes();           This works

tvAssets.ExpandAllNodes();工作,但我不想扩展所有节点,因为它包含大量节点。

而且我不能从客户端做到这一点,因为它有一些问题。

RadTreeView obj 在 JavaScript 中为 null

从服务器端展开辐射树视图

请尝试使用以下代码片段。为了绑定数据,我使用了编程数据绑定方法,但下面的代码适用于所有绑定。

.ASPX

<head runat="server">
    <title></title>
    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            var IsTreeViewLoaded;
            $(document).ready(function () {
                IsTreeViewLoaded = setInterval(function () { test() }, 500); 
            });
            function test() {
                console.log("jayesh");
                var treeView = $find("<%=RadTreeView1.ClientID%>")
                if (treeView && treeView.get_allNodes()) {
                    clearInterval(IsTreeViewLoaded);
                    var nodes = treeView.get_allNodes();
                    for (var i = 0; i < nodes.length; i++)
                        if (nodes[i].get_level() == 0 || nodes[i].get_level() == 1) {
                            if (nodes[i]._hasChildren() == true) {
                                var parentnode = nodes[i];
                                parentnode.expand();
                            }
                        }
                }
            }
        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
            <telerik:RadTreeView ID="RadTreeView1" runat="server" Width="300px" Height="350px">
            </telerik:RadTreeView>
        </div>
    </form>
</body>

ASPX.CS

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Init(object source, System.EventArgs e)
    {
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        BindToIEnumerable(RadTreeView1);
    }
    internal class SiteDataItem
    {
        private string text;
        private int id;
        private int parentId;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public int ID
        {
            get { return id; }
            set { id = value; }
        }
        public int ParentID
        {
            get { return parentId; }
            set { parentId = value; }
        }
        public SiteDataItem(int id, int parentId, string text)
        {
            this.id = id;
            this.parentId = parentId;
            this.text = text;
        }
    }
    private static void BindToIEnumerable(RadTreeView treeView)
    {
        List<SiteDataItem> siteData = new List<SiteDataItem>();
        siteData.Add(new SiteDataItem(1, 0, "Products"));
        siteData.Add(new SiteDataItem(2, 1, "Telerik UI for ASP.NET Ajax"));
        siteData.Add(new SiteDataItem(3, 1, "Telerik UI for Silverlight"));
        siteData.Add(new SiteDataItem(4, 2, "RadGrid"));
        siteData.Add(new SiteDataItem(5, 2, "RadScheduler"));
        siteData.Add(new SiteDataItem(6, 2, "RadEditor"));
        siteData.Add(new SiteDataItem(7, 3, "RadGrid"));
        siteData.Add(new SiteDataItem(8, 3, "RadMenu"));
        siteData.Add(new SiteDataItem(9, 3, "RadEditor"));
        siteData.Add(new SiteDataItem(10, 9, "RadEditor1"));
        siteData.Add(new SiteDataItem(11, 9, "RadEditor1"));
        treeView.DataTextField = "Text";
        treeView.DataFieldID = "ID";
        treeView.DataFieldParentID = "ParentID";
        treeView.DataSource = siteData;
        treeView.DataBind();
    }
}

如果有任何疑虑,请告诉我。