The scenario we ran into was this:
- Root-level tree nodes represent periods of time.
- Only the node for the current period should be expanded at load.
- Other nodes' children are lazy-loaded upon expanding (using page method call).
- We wanted to use the same method to load child nodes whether it was happening during page load or as part of a node expanding.
Here's the workaround we ended up using:
private void Page_Load()
{
// snip SQL code to load periods
using (SqlDataReader r = sqlh.ExecuteReader())
{
while (r.Read())
{
// create node for the period
RadTreeNode n = new RadTreeNode();
// if this is the current period
if (r.GetString(5)[0] == 'Y')
{
// load children
foreach (RadTreeNodeData data in GetTermNodes(residentId, termId))
{
n.Nodes.Add(NodeFromData(data));
}
n.Expanded = true;
n.ExpandMode = 0;
}
}
}
[WebMethod]
public static RadTreeNodeData[] LoadNodes(RadTreeNodeData node, object context)
{
string[] chunks = node.Value.Split('-');
if (chunks.Length < 2) return CreateNodeData("Could not get resident and term ID.");
int residentId;
if (!chunks[0].TryParseInt32(out residentId)) return CreateNodeData("Invalid resident ID.");
short termId;
if (!chunks[1].TryParseInt16(out termId)) return CreateNodeData("Invalid term ID.");
return GetTermNodes(residentId, termId).ToArray();
}
private static List<RadTreeNodeData> GetTermNodes(int residentId, short termId)
{
List<RadTreeNodeData> nodes = new List<RadTreeNodeData>();
// snip code to generate child nodes
return nodes;
}
private static RadTreeNode NodeFromData(RadTreeNodeData data)
{
RadTreeNode node = new RadTreeNode(data.Text, data.Value, data.NavigateUrl);
node.ContextMenuID = data.ContextMenuID;
node.ExpandMode = data.ExpandMode;
node.ImageUrl = data.ImageUrl;
return node;
}