I am dynamically generating a RadTreeView, and bind it to a dynamically generated XmlDataSource.
I need to set different nodes to different ItemTemplates.
To do this, I databind the RadTreeView, so each node item is generated inside the RadTreeView.Nodes collection. Then, I foreach through the Nodes collection, and set each node's template to a certain template. If I do this, the data from the XML file is overwritten with the data in the template. But if I databind the RadTreeView again afterwards, each node's template is lost again.
Here's some code :
foreach'ing through the nodes
setting the template :
The template class :
Is there another way of setting each node's template to a certain template?
I need to set different nodes to different ItemTemplates.
To do this, I databind the RadTreeView, so each node item is generated inside the RadTreeView.Nodes collection. Then, I foreach through the Nodes collection, and set each node's template to a certain template. If I do this, the data from the XML file is overwritten with the data in the template. But if I databind the RadTreeView again afterwards, each node's template is lost again.
Here's some code :
foreach'ing through the nodes
foreach (Telerik.Web.UI.RadTreeNode tn in CellTree.Nodes) |
{ |
tn.Expanded = true; |
tn.ExpandChildNodes(); |
//Set header/item templates |
SetItemTemplate(tn); |
} |
setting the template :
private void SetItemTemplate(Telerik.Web.UI.RadTreeNode Node) |
{ |
if (Node.ParentNode == null) |
{ |
MyTemplate template = new MyTemplate("Header"); |
template.InstantiateIn(Node); |
} |
else |
{ |
MyTemplate template = new MyTemplate("Item"); |
template.InstantiateIn(Node); |
} |
foreach (Telerik.Web.UI.RadTreeNode tn1 in Node.Nodes) |
{ |
//Use reflection to set each child item's template |
SetItemTemplate(tn1); |
} |
} |
The template class :
public class MyTemplate : System.Web.UI.ITemplate |
{ |
string TemplateType; |
public MyTemplate(string type) |
{ |
TemplateType = type; |
} |
public void InstantiateIn(Control container) |
{ |
PlaceHolder ph = new PlaceHolder(); |
ph.ID = "test"; |
Label label1 = new Label(); |
switch (TemplateType) |
{ |
case "Header": |
ph.Controls.Add(new LiteralControl("<div style=\"background-image:url(images/Parent.jpg); background-repeat:no-repeat; padding-left:15px; padding-top:4px; width:400px; height:31px\">")); |
label1.ID = "MenuItem"; |
label1.Text = "Text"; |
label1.Font.Size = 10; |
label1.Font.Bold = true; |
label1.ForeColor = Color.White; |
ph.Controls.Add(label1); |
ph.DataBinding += new EventHandler(ph_DataBinding); |
ph.Controls.Add(new LiteralControl("</div>")); |
break; |
case "Item": |
ph.Controls.Add(new LiteralControl("<div style=\"background-image:url(images/item.bmp); background-repeat:no-repeat; padding-left:10px; width:400px; height:24px\">")); |
label1.ID = "MenuItem"; |
label1.Text = "Text"; |
label1.Font.Size = 10; |
label1.Font.Bold = true; |
ph.Controls.Add(label1); |
ph.DataBinding += new EventHandler(ph_DataBinding); |
ph.Controls.Add(new LiteralControl("</div>")); |
break; |
} |
container.Controls.Add(ph); |
} |
private void ph_DataBinding(object sender, EventArgs e) |
{ |
PlaceHolder ph = (PlaceHolder)sender; |
Telerik.Web.UI.RadTreeNode ri = (Telerik.Web.UI.RadTreeNode)ph.NamingContainer; |
string menutext = (string)DataBinder.Eval(ri.DataItem, "Text"); |
((Label)ph.FindControl("MenuItem")).Text = menutext; |
} |
} |
Is there another way of setting each node's template to a certain template?