Having trouble getting help?
Contact Support
Contact Support
Open and close ContextMenu
21 Apr 20222 minutes to read
ContextMenu can be opened and closed programmatically whenever required by using the open and close methods.
In the following example, the ContextMenu is opened using the open
method at the specified position using top
and left
. Also, ContextMenu is closed using close
method on ContextMenu item click or document click.
<button class='e-btn' id="btnElement">Open ContextMenu</button>
<ejs-contextmenu id="contextmenu" items="ViewBag.menuItems"></ejs-contextmenu>
<script>
document.getElementById('btnElement').onclick=function() {
var contextMenuObj = ej.base.getInstance(document.getElementById('contextmenu'), ejs.navigations.ContextMenu)
contextMenuObj.open(60, 20);
}
</script>
<style>
button {
margin: 20px 0 0 5px;
}
</style>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
public class ContextMenuController : Controller
{
public ActionResult OpenClose()
{
List<object> menuItems = new List<object>();
menuItems.Add(new
{
text = "Cut"
});
menuItems.Add(new
{
text = "Copy"
});
menuItems.Add(new
{
text = "Paste"
});
ViewBag.menuItems = menuItems;
return View();
}
}
}