从服务端扩展 radTreeView

Expand radTreeView from serverside

我正在绑定来自 DatatableTelerik RadTreeView。我想从服务器端扩展 root level (node.level=0)radTreeView 节点及其子 (node.level=1)

Datatable 包含一列,其中包含节点级别的值。但是 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 is null in JavaScript

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

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();
    }

}

如有任何疑问,请告诉我。