创建自定义命令按钮

创建自定义命令按钮

在 Aspose.Cells.GridWeb 中创建自定义命令按钮:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体。
  2. 访问工作表。
  3. 创建 CustomCommandButton 类的实例。
  4. 将按钮的命令设置为某个值。该值用于按钮的事件处理程序。
  5. 设置按钮的文本。
  6. 设置按钮的图像 URL。
  7. 最后,将 CustomCommandButton 对象添加到 GridWeb 控件的 CustomCommandButtons 集合中。

代码片段的输出如下所示:

添加到 GridWeb 控件的自定义命令按钮

待办事项:图片_替代_文本

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

自定义命令按钮的事件处理

自定义命令按钮最重要的方面是它们在单击时执行的操作。要设置操作,请为 GridWeb 控件的 CustomCommand 事件创建一个事件处理程序。

单击自定义命令按钮时,始终会触发 CustomCommand 事件。因此,在将按钮添加到 GridWeb 控件时,事件处理程序必须识别命令集所应用的特定自定义命令按钮。最后,添加单击按钮时执行的自定义代码。

在下面的代码示例中,当单击按钮时,一条文本消息将添加到单元格 A1。

单击自定义命令按钮时添加到 A1 单元格的文本

待办事项:图片_替代_文本

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