Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create a jQuery Context Menu for Treeview

0.00/5 (No votes)
27 Apr 2009 1  
jQuery Context Menu for Treeview

Introduction

I looked on the internet for a context menu for a TreeView in ASP.NET, which would work like the WindowsForm TreeView. I found one, but it only worked on Internet Explorer 6. So, I didn't feel like it was a viable solution for cross browser compatibility. So, I looked at jQuery.com for some plugins and found one written by Cory S.N. LaViska.

screenshot1.jpg

screenshot2.jpg

Background

I wanted a context menu that worked with the ASP.NET TreeView, and provided the following:

  1. Cross Browser Compatible
  2. Easy to Implement
  3. Extendable enough to provide the ability to add functions to edit, delete, or add nodes to my TreeView

So, I looked at jQuery, which takes care of the cross browser compatibility. Then I found LaViska's context menu which is easy to implement, and provides the extendability I needed.

Using the Code

The code is simple. First I added the script tags to the page and the CSS file reference.

<script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.contextMenu.js" type="text/javascript"></script>
<link href="jquery.contextMenu.css" rel="stylesheet" type="text/css" />

Second, I created a TreeView control within a div as follows:

<div id="MyTreeDiv">
<asp:TreeView ID="MyTreeView" runat="server" ExpandDepth="0">
<Nodes>
 <asp:TreeNode Text="node1" Value="f2571858-0eff-4260-9935-2f53d8e1bcd0">
   <asp:TreeNode Text="node10" Value="c1ddc0fc-c170-4a74-8de3-f1e85fdb2615">
     <asp:TreeNode Text="node100" Value="c2532e51-704f-45e9-abc7-f8a2a7b1f422" />
     <asp:TreeNode Text="node101" Value="d47ce54e-1c2b-45af-b767-e610f05e0960" />
     <asp:TreeNode Text="node102" Value="faa99883-4996-4ac4-afa1-a197df615a0b" />
     <asp:TreeNode Text="node103" Value="7c49a861-c253-49c4-86a5-9b1789187f80" />
     <asp:TreeNode Text="node104" Value="42def2d5-ae7a-4746-8223-63f762de058a" />
     <asp:TreeNode Text="node105" Value="ea0051a0-1122-432e-977b-46ef1e58f9c4" />
  </asp:TreeNode>
  </asp:TreeNode>
  <asp:TreeNode Text="node2" Value="bcb02fd7-d2fa-430a-b004-1d5f8e481a9f">
   <asp:TreeNode Text="node20" Value="42516600-21c3-4866-9d45-7e42e82997c4" />
   <asp:TreeNode Text="node21" Value="3802be53-69c7-4e45-85e6-863cabea238a" />
   <asp:TreeNode Text="node22" Value="e99019d4-243e-454d-a761-0b214a4bd893" />
   <asp:TreeNode Text="node23" Value="4cbc3de2-18c0-43da-b65d-493f9a84243e" />
   <asp:TreeNode Text="node24" Value="7a211df9-6ed5-4198-b5ab-665f39069541" />
   <asp:TreeNode Text="node25" Value="862dc876-8b92-437d-9947-833faea03ce4" />
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>       

I used GUIDs for the value. This makes it easier to pass the GUID to another function or page.

Third, I created my context menu and assigned it a class of type 'contextMenu'. This class allows the menu to be hidden until I need to call it up with the jQuery code provided by the jQuery context menu, which is as follows:

<ul id="myMenu" class="contextMenu">
    <li class="copy"><a href="#add">Add</a></li>
    <li class="edit"><a href="#edit">Edit</a></li> 
    <li class="delete"><a href="#delete">Delete</a></li>
    <li class="quit separator"><a href="#cancel">Cancel</a></li>
 </ul>

Next I added the following JavaScript code to the page:

<script type="text/javascript">
$(document).ready(function() {
$("#MyTreeDiv A").contextMenu({
menu: 'myMenu'
}, function(action, el, pos) {
alert(
'Action: ' + action + '\n\n' +
'Element text: ' + $(el).text() + '\n\n' + 
'GUID: ' + getGUID($(el).attr("href")) + '\n\n' + 
'X: ' + pos.x + ' Y: ' + pos.y + ' (relative to element)\n\n' + 
'X: ' + pos.docX + ' Y: ' + pos.docY+ ' (relative to document)'
); }); });

function getGUID(mystr) {
var reGUID = /\w{8}[-]\w{4}[-]\w{4}[-]\w{4}[-]\w{12}/g 
var retArr = [];
var retval = '';
retArr = mystr.match(reGUID);
if(retArr != null)
{
retval = retArr[retArr.length - 1];
}
return retval;
}
</script>

The first function assigns the context menu 'myMenu' to my div 'MyTreeDiv', which contains my TreeView. It basically takes all the <A> tags and adds the context menu to them.
The getGUID functions gets the GUIDs from the postback function. The GUID is used to let me know what node or item to work with. I can easily pass this to another function or page to edit, delete, or add a node to my tree.

Points of Interest

I was amazed at how simple it was to use jQuery to assign a context menu to the nodes of a Treeview. I actually am using this in an application that brings up a popup using the jQuery UI popup function for each node I right click on. jQuery makes things so much easier.

History

  • 27th April, 2009: Initial post 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here