Create Custom Command Buttons

Creating Custom Command Buttons

To create a custom command button in Aspose.Cells.GridWeb:

  1. Add Aspose.Cells.GridWeb control to the web form.
  2. Access a worksheet.
  3. Create an instance of the CustomCommandButton class.
  4. Set button’s Command to some value. This value is used in the button’s event handler.
  5. Set the button’s text.
  6. Set the button’s image URL.
  7. Finally, add the CustomCommandButton object to the CustomCommandButtons collection of the GridWeb control.

The output of code snippet is shown below:

A custom command button added to GridWeb control

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a CustomCommandButton object
CustomCommandButton button = new CustomCommandButton();
// Setting the command, text and image URL for button. Image should be relative to website root folder
button.Command = "MyButton";
button.Text = "MyButton";
button.ImageUrl = "../Image/aspose.ico";
// Adding button to CustomCommandButtons collection of GridWeb
GridWeb1.CustomCommandButtons.Add(button);

Event Handling of Custom Command Button

The most important aspect of custom command buttons is the action they perform when clicked. To set the action, create an event handler for the GridWeb control’s CustomCommand event.

The CustomCommand event is always triggered when a custom command button is clicked. So the event handler has to identify the specific custom command button that it applies to by the Command set when adding the button to the GridWeb control. Finally, add custom code that is executed when the button is clicked.

In the code example below, a text message is added to the cell A1 when the button is clicked.

Text added to A1 cell when custom command button is clicked

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Creating Event Handler for CustomCommand event
protected void GridWeb1_CustomCommand(object sender, string command)
{
// Identifying a specific button by checking its command
if (command.Equals("MyButton"))
{
// Accessing the cells collection of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Putting value to "A1" cell
sheet.Cells["A1"].PutValue("My Custom Command Button is Clicked.");
// Set first column width to make the text visible
sheet.Cells.SetColumnWidth(0, 30);
}
}