Lägg till eller ta bort kontextmenyobjekt i GridWeb

Lägg till kontextmenyobjekt med ASP.NET Markup

Följande ASP.NET-markering lägger till ett snabbmenyalternativ i 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>

Här är den fullständiga ASP.NET-markeringen som skapar en GridWeb med ovanstående snabbmeny. Observera attributet OnCustomCommand=“GridWeb1_CustomCommand”. Det är händelsehanterarens namn som kommer att anropas när ditt snabbmenyalternativ klickas.

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>

Så här ser snabbmenyobjektet ut efter att ha lagts till med ovanstående ASP.NET-markering.

todo:image_alt_text

Detta är händelsehanterarens kod som exekveras när du klickar på snabbmenyalternativet. Koden kontrollerar först kommandonamnet, om det matchar vårt kommando lägger den till en text i cell A1 i det aktiva GridWeb-kalkylbladet och ställer in den första kolumnbredden till 40 enheter för att göra texten synlig.

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

Så här ser GridWeb ut när du klickar på snabbmenyn.

todo:image_alt_text

Lägg till kontextmenyobjekt i Aspose.Cells.GridWeb med hjälp av kod

Den här koden visar hur man lägger till ett snabbmenyobjekt i en GridWeb med hjälp av kod.

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

Ta bort kontextmenyobjekt i Aspose.Cells.GridWeb med hjälp av kod

Den här koden visar hur man tar bort ett snabbmenyobjekt med metoderna CustomCommandButtons.Remove() och 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);
}