I'm try to implement a contextmenu in a datagrid, i saw an event "contextmenu", I added 2 items, but how can i get the value from each item, for executing the correct method
This is demonstrated in the context menu online demo. The value is available in the event argument:
void OnMenuItemClick(MenuItemEventArgs args)
{
console.Log($"Menu item with Value={args.Value} clicked");
}
I have tried using the value of one of the menu item clicks in a condition to open a page (depending on which menu item is clicked) using ${event = 'valuename'}
and ${event.Value = 'valuename'}
but of course this gives me a mouseargs to int conversion error or a missing definition error. How can I use the value of the menu item click event to perform an action like open a dialog? I do not merely want to call and present the value, I want the value to determine the action on click.
In your grid definition
ContextMenu="ShowContextMenuWithItems"
You need to create menu by calling
protected void ShowContextMenuWithItems(MouseEventArgs args)
{
ContextMenuService.Open(args,
new List<ContextMenuItem> {
new ContextMenuItem(){ Text = "Choice 1", Value = 1 },
new ContextMenuItem(){ Text = "Choice 2", Value = 2 }
}, OnMenuItemClick);
}
protected void OnMenuItemClick(MenuItemEventArgs args)
{
if (args.Value.ToString() == "1")
// Do Something
else
// Do Other things
}
Hi @Vinod_Pillai , So this is exactly what I have designed, though my context menu is not in a grid, it is opened from a button. The issue is that an error is returned that args does not have a definition for Value, even though it should.
Pls post a sample code snippet demonstrating the issue. If you want to implement choice for button click I suggest use of split button .
Button 'click' handler
resulting error
I also tried with ${args.Value.ToString() == "framework"}
as you suggested. Both produce identical exceptions, but the statement appears logical to me.
You need to use ${item} instead of ${event} to access the clicked context menu item.
Hi @korchev, Well, dang, looking at the code behind, it appears each list component is parsed as 'item'. you are right. I will give that a shot!
@korchev, Worked perfectly, Thank you! Looking at it now, its seems obvious. I feel kind of silly for not trying that.