I am binding Telerik RadTreeView
from a Datatable
. I want to expand the radTreeView
nodes at root level (node.level=0)
and their child only (node.level=1)
from serverside.
Datatable contains a column which holds the value for node's level. But the radTreeView
doesn't expand at all.
This is the code:
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();
works but I don't want to expand all the nodes because it contains large number of nodes.
And I can't do it from clientside because it has some issues.
Please try with the below code snippet. To bind data I have used programmatic data binding method but below code works for all binding.
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();
}
}
Let me know if any concern.