カスタムコマンドボタンを作成する

カスタムコマンドボタンの作成

Aspose.Cells.GridWeb でカスタムコマンドボタンを作成するには:

  1. Aspose.Cells.GridWeb コントロールを Web フォームに追加します。
  2. ワークシートにアクセスします。
  3. CustomCommandButton クラスのインスタンスを作成します。
  4. ボタンのコマンドを特定の値に設定します。この値は、ボタンのイベントハンドラで使用されます。
  5. ボタンのテキストを設定します。
  6. ボタンの画像 URL を設定します。
  7. 最後に、CustomCommandButton オブジェクトを GridWeb コントロールの CustomCommandButtons コレクションに追加します。

コードスニペットの出力は以下に示されます:

GridWeb コントロールに追加されたカスタムコマンドボタン

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

カスタムコマンドボタンのイベント処理

カスタムコマンドボタンの最も重要な側面は、クリック時に実行するアクションです。アクションを設定するには、GridWeb コントロールの CustomCommand イベントのイベントハンドラを作成します。

CustomCommand イベントは常にカスタムコマンドボタンがクリックされたときにトリガーされます。したがって、イベントハンドラは、GridWeb コントロールにボタンを追加するときに設定されたコマンドによって適用される特定のカスタムコマンドボタンを識別する必要があります。最後に、ボタンがクリックされたときに実行されるカスタムコードを追加します。

以下のコード例では、ボタンがクリックされたときにセル A1 にテキストメッセージを追加します。

カスタムコマンドボタンがクリックされたときに A1 セルに追加されたテキスト

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