在 GridWeb 中添加或删除上下文菜单项

使用 ASP.NET 标记添加上下文菜单项

以下 ASP.NET 标记在 GridWeb 中添加上下文菜单项。

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
<CustomCommandButtons>
<acw:CustomCommandButton Command="MyContextMenuItemCommand" Text="ContextMenuItemText" CommandType="ContextMenuItem"></acw:CustomCommandButton>
</CustomCommandButtons>

这是创建具有上述上下文菜单项的 GridWeb 的完整 ASP.NET 标记。请注意 OnCustomCommand=“GridWeb1_CustomCommand” 属性。它是单击上下文菜单项时将调用的事件处理程序名称。

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
<acw:GridWeb ID="GridWeb1" runat="server" XhtmlMode="True" EnableAJAX="true" OnCustomCommand="GridWeb1_CustomCommand" EnableClientColumnOperations="False" EnableClientFreeze="False" EnableClientMergeOperations="False" EnableClientRowOperations="False" EnableStyleDialogbox="False">
<CustomCommandButtons>
<acw:CustomCommandButton Command="MyContextMenuItemCommand" Text="ContextMenuItemText" CommandType="ContextMenuItem"></acw:CustomCommandButton>
</CustomCommandButtons>
</acw:GridWeb>

这是使用上述 ASP.NET 标记添加上下文菜单项后的样子。

待办事项:图片_替代_文本

这是单击上下文菜单项时执行的事件处理程序代码。代码首先检查命令名称,如果它与我们的命令匹配,它会在活动 GridWeb 工作表的单元格 A1 中添加一个文本,并将第一列宽度设置为 40 个单位以使文本可见。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Event Handler for custom command event of GridWeb
protected void GridWeb1_CustomCommand(object sender, string command)
{
if (command.Equals("MyContextMenuItemCommand"))
{
// Accessing the active sheet
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Putting value to "A1" cell
sheet.Cells["A1"].PutValue("My Custom Context Menu Item is Clicked.");
// Set first column width to make the text visible
sheet.Cells.SetColumnWidth(0, 40);
}
}

这是单击上下文菜单项时 GridWeb 的外观。

待办事项:图片_替代_文本

使用代码在 Aspose.Cells.GridWeb 中添加上下文菜单项

此代码显示如何使用代码在 GridWeb 中添加上下文菜单项。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Init context menu item command button
CustomCommandButton cmd = new CustomCommandButton();
cmd.CommandType = CustomCommandButtonType.ContextMenuItem;
cmd.Text = "MyNewContextMenuItem";
cmd.Command = "MyNewContextMenuItemCommand";
// Add context menu item command button to GridWeb
GridWeb1.CustomCommandButtons.Add(cmd);

使用代码删除 Aspose.Cells.GridWeb 中的上下文菜单项

此代码显示如何使用 CustomCommandButtons.Remove() 和 CustomCommandButtons.RemoveAt() 方法删除上下文菜单项。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
if (GridWeb1.CustomCommandButtons.Count > 1)
{
// Remove the 2nd custom command button or context menu item using remove at method
GridWeb1.CustomCommandButtons.RemoveAt(1);
}
if (GridWeb1.CustomCommandButtons.Count >= 1)
{
// Access the 1st custom command button or context menu item and remove it
CustomCommandButton custbtn = GridWeb1.CustomCommandButtons[0];
GridWeb1.CustomCommandButtons.Remove(custbtn);
}