1

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
}
2
  • put your code for better answer.. Commented May 9, 2013 at 9:39
  • Have done but code not really relevant as its simply the OnSelectedNodeChanged event not firing when same node clicked again. Commented May 9, 2013 at 10:03

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Don't want to do this as need to display which node was selected as well, unless theres a simple way to decorate the node at the time to make it look like it's selected?
0

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.

1 Comment

Why would I remove the code that works, using this doesn't work anyway. The code works the problem is that the event is fired when the selected node is changed so when clicked again the selected node hasn't changed so the event is not fired either way.
0

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

Comments

0

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;
        }
    }
}

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.