Thomas from Germany has been helping spread the word that the adapters have hit RTM, http://blog.thomasbandt.de/de/aspnet/aspnet-2/aspnet-20-css-friendly-control-adapters-10.aspx.
Unfortunately one of Thomas' readers encountered a problem:
Albert Weinert meint: (23.11.2006 13:47:00)
Leider ist das TreeView nicht zu gebrauchen, da die SelectedNode weg ist sobald ein Postback ?ber was anderes als das TreeView ausgel?st wird. Problem ist bekannt, nicht behoben.
Basically this reports that the SelectedNode property of the adapted TreeView control improperly is null in the Load event for the Page.
I have confirmed this problem. The TreeViewAdapter is not setting the value of the SelectedNode property until its override of the RaisePostBackEvent method is called. This may reveal a very simple workaround for many developers. Let me explain.
Install the latest (RTM) version of the adapter kit. In Visual Studio, create a new web site using the template called ASP.NET CSS Friendly Web Site. Replace the contents of WalkThru\SimpleTreeView.aspx with the following:
<%@ Page Language="C#" %>
<script runat="server">
protected TreeNode FindSelectedNode(TreeNodeCollection nodes)
{
if (nodes != null)
{
foreach (TreeNode node in nodes)
{
if (node.Selected)
{
return node;
}
TreeNode childNode = FindSelectedNode(node.ChildNodes);
if (childNode != null)
{
return childNode;
}
}
}
return null;
}
protected void FixTreeView(TreeView treeView)
{
if (treeView != null)
{
TreeNode selectedNode = FindSelectedNode(treeView.Nodes);
if (selectedNode != null)
{
selectedNode.Select();
}
}
}
public void Page_PreRender(Object sender, EventArgs e)
{
MessageLabel1.Text = EntertainmentTreeView.SelectedNode == null ? "Null" : EntertainmentTreeView.SelectedNode.Text;
FixTreeView(EntertainmentTreeView);
MessageLabel2.Text = EntertainmentTreeView.SelectedNode == null ? "Null" : EntertainmentTreeView.SelectedNode.Text;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<link rel="stylesheet" href="SimpleTreeView.css" type="text/css" />
<link runat="server" rel="stylesheet" href="~/CSS/Import.css" type="text/css" id="AdaptersInvariantImportCSS" />
<!--[if lt IE 7]>
<link runat="server" rel="stylesheet" href="~/CSS/BrowserSpecific/IEMenu6.css" type="text/css" id="IEMenu6CSS" />
<![endif]-->
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="EntertainmentTreeView" runat="server" CssSelectorClass="SimpleEntertainmentTreeView" ExpandDepth="0">
<Nodes>
<asp:TreeNode Text="Music" SelectAction="Expand">
<asp:TreeNode Text="Classical" />
<asp:TreeNode Text="Rock">
<asp:TreeNode Text="Electric" />
<asp:TreeNode Text="Acoustical" />
</asp:TreeNode>
<asp:TreeNode Text="Jazz" />
</asp:TreeNode>
<asp:TreeNode Text="Movies" SelectAction="Expand">
<asp:TreeNode Text="Action" />
<asp:TreeNode Text="Drama" />
<asp:TreeNode Text="Musical" />
</asp:TreeNode>
</Nodes>
</asp:TreeView>
<div>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
</div>
<div id="EntertainmentMessage">
<asp:Label id="MessageLabel1" runat="server" />
<br />
<asp:Label id="MessageLabel2" runat="server" />
</div>
</form>
</body>
</html>
There are a couple of things to note in this sample. If you do things in Page_PreRender and you call FixTreeView then the TreeView's SelectedNode is correct. That may provide some immediate relief for the problem.
The real fix is probably to put something like FixTreeView into the adapter itself at the right point in its lifecycle. That's going to take a little more work and thinking. Can anyone confirm this workaround? Does anyone care to continue suggest where in the adapter's lifecycle we should do this fix-up? Should we be handling SelectedNode in the TreeViewAdapter in a very different way?
Russ Helfand
Groovybits.com