In Beta 2 (Will probably work if you have the Feb CTP) you can create your own TreeNode class, and if you override the RenderPreText, you can render HTML before each TreeNodes. If you override the RenderPostText method, you can render HTML after each TreeNodes.
The following is a custom TreeNode class that will render a RadioButton before the TreeNode, a CheckBox after the TreeNode and a Style attribute to the TreeNode’s Link (The displayed text), so you can specify different styles for each TreeNode:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
public class MyTreeNode : TreeNode
{
private string _nodeStyle;
protected override void RenderPreText(System.Web.UI.HtmlTextWriter writer)
{
writer.AddAttribute("type", "radio");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.AddAttribute("style", this._nodeStyle);
}
protected override void RenderPostText(System.Web.UI.HtmlTextWriter writer)
{
writer.AddAttribute("type", "checkbox");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
public string NodeStyle
{
set { this._nodeStyle = value; }
}
}
}
To use the class above, you also need to create your own custom TreeView that will use the custom TreeNode class. It can be done by creating a class that will inherits the TreeView class and override the CreateTreeNode method. The CreateTreeNode method will return the class that should represent a TreeNode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyTreeView runat=server></{0}:MyTreeView>")]
public class MyTreeView : TreeView
{
protected override TreeNode CreateNode()
{
return new MyTreeNode();
}
}
}
Now you only need to add your custom TreeView to the Page and add your TreeNode to the Nodes collection of the TreeView:
<%@ Page Language="C#" %>
<%@ Register TagPrefix="test" Namespace="Test" %>
<html>
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<test:MyTreeView ID="MyTreeView1" runat="server">
<Nodes>
<test:MyTreeNode NodeStyle="color:#ff0000" Value="New Node" Text="New Node">
<test:MyTreeNode NodeStyle="background:#00ff00" Value="New Node" Text="New Node" />
</test:MyTreeNode>
</Nodes>
</test:MyTreeView>
</div>
</form>
</body>
</html>
You can also add your node dynamically to your TreeView:
MyTreeNode node = new MyTreeNode();
node.Text = "My added TreeNode";
MyTreeView1.Nodes.Add(node);