Добавление или удаление элементов контекстного меню в 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>

Вот полная разметка ASP.NET, которая создает GridWeb с указанным выше элементом контекстного меню. Обратите внимание на атрибут 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.

дело:изображение_альтернативный_текст

Это код обработчика событий, который выполняется при нажатии на элемент контекстного меню. Код сначала проверяет имя команды, и если оно соответствует нашей команде, он добавляет текст в ячейку A1 активного рабочего листа GridWeb и устанавливает ширину первого столбца на 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);
}