Aggiungi o rimuovi voci di menu contestuale in GridWeb

Aggiungi la voce del menu di scelta rapida utilizzando il markup ASP.NET

Il markup ASP.NET seguente aggiunge una voce di menu di scelta rapida in 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>

Ecco il markup ASP.NET completo che crea un GridWeb con la voce del menu di scelta rapida sopra. Si prega di notare l’attributo OnCustomCommand=“GridWeb1_CustomCommand”. È il nome del gestore eventi che verrà chiamato quando si fa clic sulla voce del menu contestuale.

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>

Ecco come appare la voce del menu di scelta rapida dopo essere stata aggiunta utilizzando il markup ASP.NET sopra.

cose da fare:immagine_alt_testo

Questo è il codice del gestore eventi che viene eseguito quando si fa clic sulla voce del menu contestuale. Il codice controlla prima il nome del comando, se corrisponde al nostro comando, aggiunge un testo nella cella A1 del foglio di lavoro GridWeb attivo e imposta la larghezza della prima colonna su 40 unità per rendere visibile il testo.

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

Ecco come appare GridWeb quando fai clic sulla voce del menu contestuale.

cose da fare:immagine_alt_testo

Aggiungi elementi del menu contestuale in Aspose.Cells.GridWeb utilizzando il codice

Questo codice mostra come aggiungere una voce di menu di scelta rapida all’interno di un GridWeb utilizzando il codice.

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

Rimuovere le voci del menu contestuale in Aspose.Cells.GridWeb utilizzando il codice

Questo codice mostra come rimuovere la voce del menu contestuale utilizzando i metodi CustomCommandButtons.Remove() e 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);
}