ASTreeView – Use Custom Context Menu

by Weijie JIN on

ASTreeView supports context menu(right click the node). The default context menu items are Add, Edit, Delete. If you want to add your own context menu item, you can read on and follow the instructions.

The result will look like:


And it will execute some javascript for the current node.

Implementation steps:

1.  Add tree-wide context menu.

Context menu item added is tree-wide, that is all the nodes in the tree will have the menu item. I’ll introduce how to disable the item for some specified nodes later.

In the OnInit event in the page or user control, you can add the context menu item like this:

new ASContextMenuItem(
	"My Menu" //menu text
	, "alert('current value:' + " 
		+ this.astvMyTree.ContextMenuClientID 
		+ ".getSelectedItem().parentNode.getAttribute('treeNodeValue')" 
		+ ");return false;" //execution javascript
	, "text" ) ); //command name 

Here please pay attention to the command name parameter. We’ll use it to disable menu item for specified node later.

2. Disable context menu item for specified nodes.

If for some node, the context menu shouldn’t be visible, we can add an AdditionalAttribute to the node. The code:

	new KeyValuePair<string, string>( 
		"disabletext" /*disable + command name*/
		, "true" ) );

We use the pattern “disable + command name” as the value of the attribute, then add to the node.

The result will be:


The picasa node doesn’t have the “My Menu” item.

  • emerald

    Hi, how can i get the parentID after i click the context menu item? to get the CurrentID = “.getSelectedItem().parentNode.getAttribute(‘treeNodeValue’)”, how about the parent?

  • jinweijie

    hi, any question, please submit to:

  • Grisza

    I writing a project for study with my frends. How Can I add object frombase, but not in Conection string.

  • supun

    In InternetExplorer When we right click a Node it shows ContextMenu and also IE menu.

  • leszek

    hi jinweeijie

    I am a new programmer and i want to buy ASTreeView control but I need to know whether the custom context menu can trigger server side event, for example, insert record to the table.


  • jinweijie

    Hi leszek,

    Yes, it can.



  • Stan Matz

    Hello! Is there any way to change the order of existing context menu items (edit/delete/add)?

  • tejas

    hi jin, i used astreeview in my project, Basically its used for insert/delete/update functionality . but some java scipt thought errors on page. like ajax index=1 or ajaxindex=3 , so that couldn’t trace out the error, rather than its works fine. please suggest or give me some clues for fix my problem permanently.

    i am waiting for reply regoursly.

Previous post:

Next post: