ASTreeView – Use Custom Context Menu

by jinweijie on June 22, 2010

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:

image

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:

this.astvMyTree.ContextMenu.MenuItems.Add(
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:

node.AdditionalAttributes.Add(
	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:

image

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

{ 4 comments… read them below or add one }

emerald September 13, 2010 at 3:26 pm

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 September 13, 2010 at 3:37 pm

hi, any question, please submit to: http://www.astreeview.com/astreeviewdemo/Support.aspx

Grisza April 12, 2011 at 10:15 pm

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

supun November 1, 2011 at 11:56 am

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

Leave a Comment

Previous post:

Next post: