user1166905
user1166905

Reputation: 2622

Asp.Net Treeview Selecting same node doesn't fire

I have an asp.net treeview which uses the OnSelectedNodeChanged event and works ok, but if you click the same node again it doesn't fire, any ideas how to get round this?

Treeview:

<asp:TreeView ID="tvSOWASP" runat="server" ImageSet="Arrows" 
        ShowLines="True" OnTreeNodePopulate="PopulateNode" OnSelectedNodeChanged="SelectNode">
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <Nodes>
                <asp:TreeNode Expanded="True" ImageUrl="~/tree2/icons/book.gif" 
                    SelectAction="None" Text="Schemes Of Work" Value="Schemes Of Work">
                </asp:TreeNode>
            </Nodes>
            <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" 
                HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 
                HorizontalPadding="0px" VerticalPadding="0px" />    
        </asp:TreeView>

Code-Behind:

protected void SelectNode(Object sender, EventArgs e)
{
    // Code here, ok when select any node, select same node and this code is not hit
}

Upvotes: 1

Views: 3133

Answers (5)

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

I found the deselecting the current node at the end of the SelectedNodeChanged event caused problems later on during the page cycle. When the control itself rendered it wasn't showing the selected node.

Instead I added some code to the Page_Load event to clear out the current selection.

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack) {
        string eventTarget = Page.Request["__EVENTTARGET"];
        if(eventTarget == TreeView1.ClientID.Replace('_', '$')) {
            // The TreeView is posting back a selection change
            // Clear out any existing selection so that selecting the same node
            // will trigger the change event.

            TreeView1.SelectedNode.Selected = false;
        }
    }
}

Upvotes: 0

ManikandaPrabhu M
ManikandaPrabhu M

Reputation: 9

Use e.Node.Collapse(); in the treeView_TreeNodeExpanded

Upvotes: -1

Microsoft DN
Microsoft DN

Reputation: 10020

It will not fire when you click the same node again because the second time the selection is not changing so the selectednodechanged event wouldn't fire.

Please refer this link

Upvotes: 0

Rahul
Rahul

Reputation: 5636

According to me OnSelectedNodeChanged event of any control will be the ID of that control with the event name like your control name is tvSOWASP so it's event would be tvSOWASP_SelectedNodeChanged not SelectNode so change your SelectedNodeChanged event with my code like

protected void tvSOWASP_SelectedNodeChanged(object sender, EventArgs e)
{
// Your code...
}

so remove your OnSelectedNodeChanged="SelectNode" from your code and also it's click event and try to make a new event as per i mentioned.

Hope it understand and worked for you.

Upvotes: 0

Neeraj
Neeraj

Reputation: 4489

Hey Please try this one.

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){
// Do whatever you're doing
TreeView1.SelectedNode.Selected = false;
}

Hope it helps you

Upvotes: 5

Related Questions