在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标记添加上下文菜单项后的效果。

todo:image_alt_text

这是当单击上下文菜单项时执行的事件处理程序代码。代码首先检查命令名称,如果符合我们的命令,它会在活动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的外观。

todo:image_alt_text

使用代码在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);
}