eMTeeN
eMTeeN

Reputation: 223

Calling a method when a tree node is clicked in the Standard ASP.NET 2.0 TreeView

I have a treeview which i populate dynamically using an XML Datasource. The leaf nodes in the TreeView attempt to open a URL in an iframe within the page.

This all works fine, but i would like the iframe to be hidden until the point the leaf node is selected.

Does anyone know what event is triggered when the nodes are clicked?? I tried the SelectedNodeChanged event but this doesnt seem to get triggered!

Or is there any other way to do this??

UPDATE - The TreeView code is shown Below

<asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" DataSourceID="XmlDataSource2" AutoGenerateDataBindings="False">
  <DataBindings>
     <asp:TreeNodeBinding DataMember="Node"  NavigateUrlField="URL" ValueField="Name" TargetField="iframe" SelectAction="Select" />         
  </DataBindings>
</asp:TreeView>

While fiddling with my code i noticed that when i remove the NavigateUrlField="URL" from my code the tree triggers the SelectedNodeChange event, But does not Trigger if NavigateUrlField="URL" is put back in.

Any idea as to how i can get around this???

Upvotes: 1

Views: 6921

Answers (2)

Cerebrus
Cerebrus

Reputation: 25775

The SelectedNodeChanged is the correct event to trap in your scenario. Perhaps a little more detail might help to identify why this event is not raised.

Also, you should keep in mind that it is possible that the TreeView may not raise the event at all unless the SelectAction for the node is set to Select or SelectExpand. This might be a point to look into.

Edit: (after OP's update to original question):


This behaviour is quite natural. When you set the NavigateUrl of a TreeNode, it goes into "Navigation mode". This means that it renders as a hyperlink instead of a clickable node. Therefore, all selection events are disabled for such a TreeNode and the SelectedNode property of the TreeView will return a null reference. This is because the purpose of the TreeNode becomes solely the redirection to a supplied URL.

Now, there are a few solutions to solve this problem:

a. Instead of setting a NavigateUrl property declaratively, handle the SelectedNodeChanged event and set the IFrame's src attribute there conditionally depending on which node was clicked (the SelectedNode). Also set the IFrame's visibility to True.

b. Attach a client script to the onclick event of your TreeView within which you set the visibility of the IFrame. Examples on how to do this are on Madhur Ahuja's blog and here.

Hope this helps! :-)

Upvotes: 4

grover
grover

Reputation: 2295

The AfterSelect event fires after the node selection has changed. The BeforeSelect right before the selection in the tree changes.

Upvotes: 0

Related Questions