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

RadTreeView.DataBind() not working

3 Answers 377 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Peter Blair
Top achievements
Rank 1
Peter Blair asked on 10 May 2010, 06:21 PM

The data binding event in this code isn't working any longer.  I was testing a custom node template and using the code from this page (add and edit templates Runtime), but then removed all the code specific to the custom node template.  At that point, the data stopped binding.  I have added the code back, moved all of it to another page, but the data binding event doesn't seem to fire.  Even with all the test code from the linked page above in my page, if I set a breakpoint in the DataBinding function it never gets hit.  If I manually place a RadTreeView on the .aspx page it will display, but anything set on the server side will not.

I have checked to make sure List used to populate the tree has data, and this line (radTree.DataSource = treeNodes) works correctly.  But the databind is still failing.

Any suggestions?

Thanks,
Peter

namespace FQRSurvey.Controls  
{  
   public partial class TreeViewControl : System.Web.UI.UserControl  
   {  
      protected void Page_Load(object sender, EventArgs e)  
      {  
         if (!this.Page.IsPostBack)  
         {  
            //load tree view  
            Survey survey = SurveyController.GetSurveyByID(new Guid("6C685FBA-1380-47B7-A911-DC89AF9479CF"));  
            CreateRadTree(survey, questionTree);  
         }  
      }  
      protected override void OnInit(EventArgs e)  
      {  
         questionTree.NodeTemplate = new SectionTemplate();  
         //questionTree.NodeTemplate = new QuestionTemplate();  
         //questionTree.NodeTemplate = new AnswerTemplate();  
         base.OnInit(e);  
      }  
 
      protected void RadTreeView1_HandleDrop(object sender, RadTreeNodeDragDropEventArgs e)  
      {  
         RadTreeNode sourceNode = e.SourceDragNode;  
         RadTreeNode destNode = e.DestDragNode;  
         RadTreeViewDropPosition dropPosition = e.DropPosition;  
 
         if (destNode != null)  
         {  
            if (sourceNode.TreeView.SelectedNodes.Count <= 1)  
            {  
               if (!sourceNode.IsAncestorOf(destNode))  
               {  
                  sourceNode.Owner.Nodes.Remove(sourceNode);  
                  destNode.Nodes.Add(sourceNode);  
               }  
            }  
 
            destNode.Expanded = true;  
            sourceNode.TreeView.ClearSelectedNodes();  
         }  
      }  
 
      private void CreateRadTree(Survey survey, RadTreeView radTree)  
      {  
         radTree.NodeTemplate = new SectionTemplate();  
 
         if (!survey.Sections.Count.Equals(0))  
         {  
            List<TreeNode1> treeNodes = new List<TreeNode1>();  
            Sections sections = survey.Sections;  
 
            //Loop through Sections  
            for (int i = 0; i < sections.Count; i++)  
            {  
               Section section = sections[i];  
               //Create Section Node  
               TreeNode1 sNode = new TreeNode1(section.SectionID.ToString(), String.Empty, section.SectionTitle, section.SectionStatus);  
               treeNodes.Add(sNode);  
 
               //Get Questions  
               if (section.FirstQuestion != null)  
               {  
                  Question q = section.FirstQuestion;  
                  string prevNode = String.Empty;  
 
                  if (q.PrevAnswer == null) prevNode = sNode.ID;  
                  else prevNode = q.PrevAnswer.AnswerID.ToString();  
 
                  //Create Question Node  
                  TreeNode1 qNode = new TreeNode1("q" + q.FriendlyID, prevNode, q.QuestionText, SurveyStatus.Active);  
                  //Create Answer Group  
                  treeNodes.Add(qNode);  
               }  
            }  
 
            radTree.DataTextField = "Text";  
            radTree.DataFieldID = "ID";  
            radTree.DataFieldParentID = "ParentID";  
            radTree.DataSource = treeNodes;  
            radTree.DataBind();  
         }  
      }  
   }  
 
 
   internal class TreeNode1  
   {  
      private string _id;  
      private string _text;  
      private SurveyStatus _status;  
      private string _parentID;  
 
      public string ID  
      {  
         get { return _id; }  
         set { _id = value; }  
      }  
 
      public string ParentID  
      {  
         get { return _parentID; }  
         set { _parentID = value; }  
      }  
 
      public string Text  
      {  
         get { return _text; }  
         set { _text = value; }  
      }  
 
      public SurveyStatus Status  
      {  
         get { return _status; }  
         set { _status = value; }  
      }  
 
      public TreeNode1(string id, string parentID, string text, SurveyStatus status)  
      {  
         _id = id;  
         _text = text;  
         _status = status;  
         _parentID = parentID;  
      }  
   }  
 
   class SectionTemplate : ITemplate  
   {  
      public void InstantiateIn(Control container)  
      {  
         Label label1 = new Label();  
         label1.Font.Size = 15;  
         label1.DataBinding += new EventHandler(label1_DataBinding);  
         container.Controls.Add(label1);  
      }  
 
      private void label1_DataBinding(object sender, EventArgs e)  
      {  
         Label target = (Label)sender;  
         RadTreeNode node = (RadTreeNode)target.BindingContainer;  
         string nodeText = (string)DataBinder.Eval(node, "Text");  
         target.Text = nodeText;  
      }  
   }  
 


Update:
From additional digging and messing around, the problem relates to converting a Guid to a string to be used as the ID field.  I'm not sure why it won't accept the Guid as a string, but using an int instead of string fixes the problem.  Any reason convertinting a Guid to a string would cause the data to fail to bind without any errors?

3 Answers, 1 is accepted

Sort by
0
Nikolay Tsenkov
Telerik team
answered on 11 May 2010, 11:40 AM
Hello Peter Blair,

I took some time to adjust your code (I don't have the entire code - Section type etc.) for debugging and I think that the problem you are having is not caused by the template. I think that your method CreateTree creates somewhere a node with wrong ParentID. This would explain, why the binding is not happening.

You said that "have checked to make sure List used to populate the tree has data, and this line (radTree.DataSource = treeNodes) works correctly", try after that line to investigate is there a wrong ParentID on the elements (wrong is when there is no such node, for example: ParentID="1" and there is no node with ID="1", if the node is root than it should be "null").

Also I see that you apply the template in the OnInit method and in CreateTree method. It should be only applied in OnInit (since it's called on first load and on every postback always before Page_Load).

Hope this will help to solve your problem!


Regards,
Nikolay Tsenkov
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.
0
Robin
Top achievements
Rank 1
answered on 14 Feb 2012, 08:01 AM
hi,

please refer to TreeView / Programmatic Data Binding Sample

instead of using int
            public int ID
            {
                get { return _id; }
                set { _id = value; }
            }

please change to string 
            public string ID
            {
                get { return _id; }
                set { _id = value; }
            }

and the TreeView does not work anymore. May you help to figure out why?

PS: my scenario is simple: I define some Permission in XML file. The permission key is string.

When creating/modifying a new user, the admin can assign some permissions from the Permission Tree.

So, programmatically, I load the XML file to a List<PermissionItem> and bind the List to TreeView datasource, but it does not work, nothing is shown.

0
Robin
Top achievements
Rank 1
answered on 14 Feb 2012, 08:17 AM
after looking at the documentation http://www.telerik.com/help/aspnet-ajax/treeview-data-binding-array.html

I found the reason (in case the id is string or guid): null should be used for ParentID instead of string.Empty or Guid.Empty

Regards,
Tags
TreeView
Asked by
Peter Blair
Top achievements
Rank 1
Answers by
Nikolay Tsenkov
Telerik team
Robin
Top achievements
Rank 1
Share this question
or