从服务器端展开radTreeView

时间:2015-06-17 18:44:18

标签: c# asp.net telerik radtreeview

我从Telerik RadTreeView绑定Datatable。我想从服务器端展开radTreeView的{​​{1}}节点及其子节点root level (node.level=0)

数据表包含一个保存节点级别值的列。但(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 有效,但我不想扩展所有节点,因为它包含大量节点。

我无法从客户端进行此操作,因为它存在一些问题。

RadTreeView obj is null in JavaScript

1 个答案:

答案 0 :(得分:1)

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

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

}

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