カスタムコマンドボタンを作成する
カスタムコマンドボタンの作成
Aspose.Cells.GridWeb でカスタムコマンドボタンを作成するには:
- Aspose.Cells.GridWeb コントロールを Web フォームに追加します。
- ワークシートにアクセスします。
- CustomCommandButton クラスのインスタンスを作成します。
- ボタンのコマンドを特定の値に設定します。この値は、ボタンのイベントハンドラで使用されます。
- ボタンのテキストを設定します。
- ボタンの画像 URL を設定します。
- 最後に、CustomCommandButton オブジェクトを GridWeb コントロールの CustomCommandButtons コレクションに追加します。
コードスニペットの出力は以下に示されます:
GridWeb コントロールに追加されたカスタムコマンドボタン
カスタムコマンドボタンのイベント処理
カスタムコマンドボタンの最も重要な側面は、クリック時に実行するアクションです。アクションを設定するには、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); | |
} | |
} |