This is a migrated thread and some comments may be shown as answers.

Treeview Template Error

1 Answer 48 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Kevin Mauriello
Top achievements
Rank 1
Kevin Mauriello asked on 03 May 2010, 12:59 AM
I am building a treeview with a grid like template (it may also become a listview rather than a grid).  When I load the non-templated nodes via .aspx it works fine, when I try do build the tree dynamically on server side it fails with the following error:

The DataSourceID of 'ctl00' must be the ID of a control of type IDataSource.  A control with ID 'Level 0' could not be found

I am not sure what I am doing wrong, any assistance would be appreciated.

I have attached a picture of what the correct display.

Working code:
<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<script runat="server"
    protected override void OnInit(EventArgs e) 
    { 
        RadTreeView1.NodeTemplate = new RadGridTemplate(); 
        base.OnInit(e); 
    } 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            foreach (RadTreeNode node1 in RadTreeView1.GetAllNodes()) 
            { 
                if (node1.Level == 1) 
                    node1.Nodes.Add(new RadTreeNode("SqlDataSource1")); 
            } 
        } 
        RadTreeView1.DataBind(); 
    } 
 
    class RadGridTemplate : ITemplate 
    { 
        public void InstantiateIn(Control container) 
        { 
            RadGrid RadGrid1 = new RadGrid(); 
            RadGrid1.DataBinding += new EventHandler(RadGrid_DataBinding); 
            container.Controls.Add(RadGrid1); 
        } 
        private void RadGrid_DataBinding(object sender, EventArgs e) 
        { 
            RadGrid target = (RadGrid)sender; 
            RadTreeNode node = (RadTreeNode)target.BindingContainer; 
            string source = (string)DataBinder.Eval(node, "Text"); 
            target.DataSourceID = source
        } 
    } 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
        <telerik:RadTreeView ID="RadTreeView1" Runat="server"
        <Nodes>  
              <telerik:RadTreeNode Text="Level 0"
                <Nodes> 
                    <telerik:RadTreeNode Text="Level 1a" /> 
                    <telerik:RadTreeNode Text="Level 1b" /> 
                    <telerik:RadTreeNode Text="Level 1c" /> 
                    <telerik:RadTreeNode Text="Level 1d" /> 
                    <telerik:RadTreeNode Text="Level 1e" /> 
                    <telerik:RadTreeNode Text="Level 1f" /> 
                </Nodes> 
              </telerik:RadTreeNode>  
        </Nodes> 
        </telerik:RadTreeView>  
    </form> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GolfLeague %>" 
        SelectCommand="select * from scores where memberid = 1" 
        SelectCommandType="Text"
    </asp:SqlDataSource> 
</body> 
</html> 
 

Not Working Code:
<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<script runat="server"
    protected override void OnInit(EventArgs e) 
    { 
        RadTreeView1.NodeTemplate = new RadGridTemplate(); 
        base.OnInit(e); 
    } 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            LoadTree(); 
            foreach (RadTreeNode node1 in RadTreeView1.GetAllNodes()) 
            { 
                if (node1.Level == 1) 
                    node1.Nodes.Add(new RadTreeNode("SqlDataSource1")); 
            } 
        } 
        RadTreeView1.DataBind(); 
    } 
 
    protected void LoadTree() 
    { 
        // 
        // ORIGINAL HIERACHICAL LOADING OF TREEVIEW 
        // 
        // get the hierachical data from the DAL 
        //DataTable dt = DAL.Routines.getFlightPlayerTree(); 
 
        //treeView.DataTextField = "Name"
        //treeView.DataFieldID = "ID"
        //treeView.DataValueField = "ID"
        //treeView.DataFieldParentID = "ParentID"
        //treeView.DataSource = dt
        RadTreeView1.Nodes.Clear(); 
        RadTreeNode node = new RadTreeNode("Level 0"); 
        node.Nodes.Add(new RadTreeNode("Level 1a")); 
        node.Nodes.Add(new RadTreeNode("Level 1b")); 
        node.Nodes.Add(new RadTreeNode("Level 1c")); 
        node.Nodes.Add(new RadTreeNode("Level 1d")); 
        node.Nodes.Add(new RadTreeNode("Level 1e")); 
        node.Nodes.Add(new RadTreeNode("Level 1f")); 
        RadTreeView1.Nodes.Add(node); 
    } 
 
    class RadGridTemplate : ITemplate 
    { 
        public void InstantiateIn(Control container) 
        { 
            RadGrid RadGrid1 = new RadGrid(); 
            RadGrid1.DataBinding += new EventHandler(RadGrid_DataBinding); 
            container.Controls.Add(RadGrid1); 
        } 
        private void RadGrid_DataBinding(object sender, EventArgs e) 
        { 
            RadGrid target = (RadGrid)sender; 
            RadTreeNode node = (RadTreeNode)target.BindingContainer; 
            string source = (string)DataBinder.Eval(node, "Text"); 
            target.DataSourceID = source
        } 
    } 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" /> 
        <telerik:RadTreeView ID="RadTreeView1" Runat="server"
        </telerik:RadTreeView>  
    </form> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GolfLeague %>" 
        SelectCommand="select * from scores where memberid = 1" 
        SelectCommandType="Text"
    </asp:SqlDataSource> 
</body> 
</html> 
 

1 Answer, 1 is accepted

Sort by
0
Accepted
Yana
Telerik team
answered on 03 May 2010, 03:41 PM
Hello Kevin,

The problem is that you set the template globally for all the nodes, so it tries to add the grid for the first node with text  "Level 0" and searches for a datasource with such ID, hence the error.

You can define the template in Page_Load event only for some nodes as it's demonstrated in our help article here:

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       RadTreeView1.Nodes.Add(new RadTreeNode("root1"));
       RadTreeView1.Nodes.Add(new RadTreeNode("root2"));           
   }
   TextBoxTemplate template = new TextBoxTemplate();
   foreach (RadTreeNode node in RadTreeView1.GetAllNodes())
   {
       template.InstantiateIn(node);
   }
   RadTreeView1.DataBind();
}


Regards,
Yana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Kevin Mauriello
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or