创建自定义命令按钮
Contents
[
Hide
]
Aspose.Cells.GridWeb 包含特殊按钮,如提交, 救球和撤消.所有这些按钮都为 Aspose.Cells.GridWeb 执行特定任务。
还可以添加执行自定义任务的自定义按钮。本主题说明如何使用此功能。
创建自定义命令按钮
在 Aspose.Cells.GridWeb 中创建自定义命令按钮:
- 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体。
- 访问工作表。
- 创建 CustomCommandButton 类的实例。
- 将按钮的命令设置为某个值。该值用于按钮的事件处理程序。
- 设置按钮的文本。
- 设置按钮的图像 URL。
- 最后,将 CustomCommandButton 对象添加到 GridWeb 控件的 CustomCommandButtons 集合中。
还可以使用 Visual Studio 的“属性”对话框以所见即所得模式添加自定义命令按钮。
代码片段的输出如下所示:
添加到 GridWeb 控件的自定义命令按钮
自定义命令按钮的事件处理
自定义命令按钮最重要的方面是它们在单击时执行的操作。要设置操作,请为 GridWeb 控件的 CustomCommand 事件创建一个事件处理程序。
单击自定义命令按钮时,始终会触发 CustomCommand 事件。因此,在将按钮添加到 GridWeb 控件时,事件处理程序必须识别命令集所应用的特定自定义命令按钮。最后,添加单击按钮时执行的自定义代码。
在下面的代码示例中,当单击按钮时,一条文本消息将添加到单元格 A1。
单击自定义命令按钮时添加到 A1 单元格的文本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |