ComponentOne TreeView for ASP.NET: TreeView for ASP.NET AJAX Task-Based Help > Preventing C1TreeViewNode from Losing its Selected State

Preventing C1TreeViewNode from Losing its Selected State

When you click on a node that contains a link, a page redirect occurs which causes the causes the current node in the C1TreeView to lose its selected state.

To prevent the C1TreeViewNode from losing its selected state when you click on a node that contains a url link, use the FindNodeByNavigateUrl method.

Complete the following steps to prevent the C1TreeViewNode from losing its selected state when you click on a node that contains a URL link:

1.   Declare the C1TreeView using the following markup.

<div>

        <a href="http://localhost:1196/ControlExplorer/C1TreeView/NavigateTo.aspx?Id=5">Navigate to NavigateTo</a>

</div>

   

    <div>

        <c1:C1TreeView ID="NavigateTo"

        VisualStylePath = "~/C1TreeView/VisualStyles/"

        AutoPostBack="True"

        ShowExpandCollapse="False"

        runat="server" AllowSorting="False" AutoCollapse="False"

        VisualStyle="Default" Width="300px">

            <Nodes>

                <c1:C1TreeViewNode Text="NavigateFrom" NavigateUrl="NavigateFrom.aspx" runat="server"></c1:C1TreeViewNode>

                <c1:C1TreeViewNode Text="NavigateTo" NavigateUrl="NavigateTo.aspx?Id=5" runat="server"></c1:C1TreeViewNode>

            </Nodes>           

        </c1:C1TreeView>

    </div>

2.   Use the FindNodeByNavigateUrl method to search for the NavigateUrl in the C1TreeViewNode collection. If the node isn’t empty then the selected state is applied to the C1TreeViewNode.

      Visual Basic

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Dim url As String = Request.Url.PathAndQuery

    Dim node As C1TreeViewNode = NavigateTo.FindNodeByNavigateUrl(url)

    If node IsNot Nothing Then

        node.Selected = True

    End If

End Sub

      C#

protected void Page_Load(object sender, EventArgs e)

    {

        string url = Request.Url.PathAndQuery;

        C1TreeViewNode node = NavigateTo.FindNodeByNavigateUrl(url);

        if(node != null)

            node.Selected = true;

    }


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.