Hinzufügen oder Entfernen von Kontextmenüelementen in GridWeb

Kontextmenüelement mit ASP.NET-Markup hinzufügen

Das folgende ASP.NET-Markup fügt ein Kontextmenüelement in GridWeb hinzu.

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>

Hier ist das vollständige ASP.NET-Markup, das ein GridWeb mit dem obigen Kontextmenüelement erstellt. Bitte beachten Sie das Attribut OnCustomCommand=“GridWeb1_CustomCommand”. Es ist der Name des Ereignishandlers, der aufgerufen wird, wenn auf Ihr Kontextmenüelement geklickt wird.

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>

So sieht das Kontextmenüelement aus, nachdem es mit dem obigen ASP.NET-Markup hinzugefügt wurde.

todo: Bild_alt_Text

Dies ist der Event-Handler-Code, der ausgeführt wird, wenn auf das Kontextmenüelement geklickt wird. Der Code überprüft zuerst den Befehlsnamen, wenn er mit unserem Befehl übereinstimmt, fügt er einen Text in Zelle A1 des aktiven GridWeb-Arbeitsblatts hinzu und setzt die erste Spaltenbreite auf 40 Einheiten, um Text sichtbar zu machen.

// 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);
}
}

So sieht das GridWeb aus, wenn Sie auf den Kontextmenüpunkt klicken.

todo: Bild_alt_Text

Fügen Sie mithilfe von Code Kontextmenüelemente in Aspose.Cells.GridWeb hinzu

Dieser Code zeigt, wie Sie mithilfe von Code ein Kontextmenüelement in einem GridWeb hinzufügen.

// 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);

Entfernen Sie Kontextmenüelemente in Aspose.Cells.GridWeb mit Code

Dieser Code zeigt, wie ein Kontextmenüelement mithilfe der Methoden CustomCommandButtons.Remove() und CustomCommandButtons.RemoveAt() entfernt wird.

// 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);
}