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

Can't bind a treeview when it has a node template.

5 Answers 210 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Iron
Andy asked on 10 Nov 2011, 06:06 PM
I can bind a array (containing hierarchical data) to a treeview with no problem.  I've been able to use a template when I've created the nodes programically.

I can't seem to be able to bind the array to a tree veiw that has a node template:

Error:
DataBinding: 'Telerik.Web.UI.RadTreeNode' does not contain a property with the name 'user'.

code:

<telerik:RadTreeView ID="RadTreeView1" runat="server">
   <NodeTemplate>
      <%# DataBinder.Eval(Container, "user")%> 
        <br />
     <%# DataBinder.Eval(Container, "mess") %> 
        <br />
     <%# DataBinder.Eval(Container, "dateentry") %> 
   </NodeTemplate>
</telerik:RadTreeView>

public class messages
   {
   public int id { get; set; }
   public int pid { get; set; }
   public string user { get; set; }
   public DateTime dateentry { get; set; }
   public string mess { get; set; }
   public messages(int i, int p, string u, DateTime dt, string m)
      {
      id = i;
      pid = p;
      user = u;
      dateentry = dt;
      if (m.Length > 300)
         mess = m.Substring(0, 290);
      else
         mess = m;
      }
   }
ArrayList data = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
   {
   if (IsPostBack)
      {
      data = (ArrayList)Session["Data4"];
      }
   else
      {
      data = new ArrayList();
      LoadData();
      Session["Data4"] = data;
      }
   RadTreeView1.DataSource = data;
   RadTreeView1.DataTextField = "mess";
   RadTreeView1.DataValueField = "id";
   RadTreeView1.DataNavigateUrlField = "";
   RadTreeView1.DataFieldID = "id";
   RadTreeView1.DataFieldParentID = "pid";
   RadTreeView1.DataBind();
   }


5 Answers, 1 is accepted

Sort by
0
Andy
Top achievements
Rank 1
Iron
answered on 11 Nov 2011, 12:50 AM
Ok... this problem is solved.  The binding format needs to be

<%

# Eval("user")%>

 


( I got the other format from the help pages somewhere)

However I'm still having issues with the TemplateNeeded() callback.

e.Node.Value and e.Node.Text are not set.  This surprises me because this example uses e.Node.Value:

http://www.telerik.com/help/aspnet-ajax/e_telerik_web_ui_radtreeview_templateneeded.html

The only valid value seems to be e.Node.Level which isn't enough information for what I'm trying to do.  ( I want to have a textbox as the last left of my 2nd level )

Can I set the template in another place such as in the NodeDataBound() function?
0
Plamen
Telerik team
answered on 14 Nov 2011, 06:05 PM
Hello Andy,

You can refer to the Adding and Editing Templates at Runtime help topic. If the RadTreeView is bound to a data source the templates can be added in the NodeDateBound server event.

You can also refer to the this help article where is shown how to get the value of an additional column.

Hope this will be helpful.

Kind regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Andy
Top achievements
Rank 1
Iron
answered on 14 Nov 2011, 06:29 PM
<<If the RadTreeView is bound to a data source the templates can be added in the NodeDateBound server event>>

Are you sure about this?  This is what I want but it doesn't seem to be working.

Here is my databound event:

protected void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e)
   {
   messages m = (messages)e.Node.DataItem;// Displaydata[index];
   if (m.show)
      e.Node.Visible = true;
   else
      e.Node.Visible = false;
   e.Node.Expanded = true;
     
   ////// Apply Template
   if (m.nodetype == "P")
      {
      ITemplate t = LoadTemplate("~/UserControls/MainComment.ascx");
      e.Node.NodeTemplate = t;
      }
   if (m.nodetype == "C")
      {
      ITemplate t = LoadTemplate("~/UserControls/CommentReply.ascx");
      e.Node.NodeTemplate = t;
      }
   if (m.nodetype == "SA")
      {
      ITemplate t = LoadTemplate("~/UserControls/ShowAll.ascx");
      e.Node.NodeTemplate = t;
      }
   }

This does not work.

The same code works in the TemplateNeeded() event however since value is not set I dont' know which template to apply

 

0
Plamen
Telerik team
answered on 16 Nov 2011, 05:14 PM
Hello Andy,

Here is a sample project that shows what I had in mind.

Hope this will be helpful.

Kind regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Andy
Top achievements
Rank 1
Iron
answered on 16 Nov 2011, 06:14 PM
Your example worked.  I'm using a User Control so I modified it to this:

 

ITemplate t = LoadTemplate("~/UserControls/MainComment.ascx");

 

t.InstantiateIn(e.Node);


It kinda worked.  The controls in the template worked but none of the bound fields in the User Control had data.

I'm likely going to go a different direction then a treeview so I'm not going to worry about this anymore.


Tags
TreeView
Asked by
Andy
Top achievements
Rank 1
Iron
Answers by
Andy
Top achievements
Rank 1
Iron
Plamen
Telerik team
Share this question
or