Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear the previous selection when an asp:TreeView is in an UpdatePanel?

I have an ASP.Net 2.0 page that contains two UpdatePanels. The first panel contains a TreeView. The second panel contains a label and is triggered by a selection in the tree. When I select a node the label gets updated as expected and the TreeNode that I clicked on becomes highlighted and the previously selected node is no longer highlighted. However if a node is original highlighted(selected) in the code-behind the highlight is not removed when selecting another node.

The markup

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
         <SelectedNodeStyle BackColor="Pink" />
      </asp:TreeView>
   </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="True">
   <ContentTemplate>
      <asp:Label ID="Label1" runat="server" Text=" - "></asp:Label>
   </ContentTemplate>
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="TreeView1" EventName="SelectedNodeChanged" />
   </Triggers>
</asp:UpdatePanel>

The code behind

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      TreeView1.Nodes.Add(new TreeNode("Test 1", "Test One"));
      TreeView1.Nodes.Add(new TreeNode("Test 2", "Test Two"));
      TreeView1.Nodes.Add(new TreeNode("Test 3", "Test Three"));
      TreeView1.Nodes.Add(new TreeNode("Test 4", "Test Four"));
      TreeView1.Nodes[0].Selected = true;
   }
}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
   Label1.Text = TreeView1.SelectedValue;
}

The at the start the first node is selected. Why is its highlight not removed when selecting another node?

Also, I asked a different question about the same setup that I haven't got an answer for. Any help would appreciated.

Edit I know that setting ChildrenAsTriggers="false" will work but I want to avoid rendering the tree again as it can be very large.

like image 844
tpower Avatar asked Dec 04 '25 20:12

tpower


2 Answers

    /// <summary>
    /// Remove selection from TreeView
    /// </summary>
    /// <param name="tree"></param>
    public static void ClearTreeView(TreeView tree)
    {

        if (tree.SelectedNode != null)
        {
            tree.SelectedNode.Selected = false;
        }
    }
like image 74
Newborn Avatar answered Dec 06 '25 12:12

Newborn


You need to set the selection to false for all nodes.

I use something like this for one of my applications (with my treeview tvCategories):

public void RefreshSelection(string guid)
{
    if (guid == string.Empty)
        ClearNodes(tvCategories.Nodes);
    else
        SelectNode(guid, tvCategories.Nodes);

}

private void ClearNodes(TreeNodeCollection tnc)
{
    foreach (TreeNode n in tnc)
    {
        n.Selected = false;
        ClearNodes(n.ChildNodes);
    }
}
private bool SelectNode(string guid, TreeNodeCollection tnc)
{
    foreach (TreeNode n in tnc)
    {
        if (n.Value == guid)
        {
            n.Selected = true;
            return true;
        }
        else
        {
            SelectNode(guid, n.ChildNodes);
        }
    }

    return false;
}
like image 40
Jason Kealey Avatar answered Dec 06 '25 13:12

Jason Kealey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!