【セル型】 ボタン型セルをクライアント側で処理したい

文書番号 : 21820     文書種別 : 使用方法     最終更新日 : 2006/06/23
文書を印刷する
対象製品
SPREAD for .NET 2.5J Web Forms Edition
詳細
製品で提供されるButtonCellTypeはonClickイベントでポストバックし、サーバー側でButtonCommandイベントを発生させます。

ただButtonCellTypeクラスを継承するカスタムボタン型クラスを作成することでonClickイベントをクライアント側で処理することができます。

  メモメモ
  • 既存クラスの継承は.NETアプリケーション開発における一般的な手法です。継承方法の詳細についてはMSDNライブラリをご参照ください。(既存クラスのカスタマイズ方法については弊社サポートサービス対象外となります)


ここでは2種類のカスタムボタン型セルクラスを紹介します。

MyButtonCell1クラスはonClickイベントをクライアント側でのみ処理し、サーバー側にポストバックしません。

MyButtonCell2クラスはonClickイベントをまずは、クライアント側で処理し、その後でサーバー側にポストバックしButtonCommandイベントを発生させます。

【VB.NETサンプルコード】
-------------------------------
Webフォームクラス
-------------------------------
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If IsPostBack Then
      Return
    End If

    Dim bc1 As New MyButtonCell1
    bc1.Text = "Button1"
    FpSpread1.Sheets(0).Cells(0, 0).CellType = bc1

    Dim bc2 As New MyButtonCell2
    bc2.Text = "Button2"
    bc2.CommandName = "I'm btn2"
    FpSpread1.Sheets(0).Cells(1, 0).CellType = bc2
  End Sub

  Private Sub FpSpread1_ButtonCommand(ByVal sender As Object, ByVal e As FarPoint.Web.Spread.SpreadCommandEventArgs) Handles FpSpread1.ButtonCommand
    'MyButtonCell2で発生するButtonCommandイベントを処理する
    If e.CommandName = "I'm btn2" Then
      'クリックされたボタンの位置を取得
      Dim cell As System.Drawing.Point = CType(e.CommandArgument, System.Drawing.Point)
      e.SheetView.Rows(cell.X).ForeColor = Color.Red '押されたボタンの行で文字色を変更
    End If
  End Sub

-------------------------------
MyButtonCell1クラス
-------------------------------
Imports FarPoint.Web.Spread

'このボタン型セルはサーバー側にポストバックしません
<Serializable()> Public Class MyButtonCell1
  Inherits ButtonCellType

  Public Overrides Function PaintCell(ByVal id As String, ByVal parent As System.Web.UI.WebControls.TableCell, ByVal style As FarPoint.Web.Spread.Appearance, ByVal margin As FarPoint.Web.Spread.Inset, ByVal value As Object, ByVal upperLevel As Boolean) As System.Web.UI.Control
    Dim c As Control = MyBase.PaintCell(id, parent, style, margin, value, upperLevel)
    Dim b As Button = CType(c, Button)
    b.Attributes.Add("onclick", "onMyButtonClick()return false")
    Return b
  End Function

End Class
-------------------------------
MyButtonCell2クラス
-------------------------------
Imports FarPoint.Web.Spread

'このボタン型セルはスクリプトを実行後、サーバー側にポストバックします(FpSpread.ButtonCommandイベントが発生します)
<Serializable()> Public Class MyButtonCell2
  Inherits ButtonCellType

  Public Overrides Function PaintCell(ByVal id As String, ByVal parent As System.Web.UI.WebControls.TableCell, ByVal style As FarPoint.Web.Spread.Appearance, ByVal margin As FarPoint.Web.Spread.Inset, ByVal value As Object, ByVal upperLevel As Boolean) As System.Web.UI.Control
    Dim c As Control = MyBase.PaintCell(id, parent, style, margin, value, upperLevel)
    Dim b As Button = CType(c, Button)
    'ボタンに設定されたスクリプトを退避
    Dim jscript As String = b.Attributes("onclick")
    b.Attributes.Add("onclick", "onMyButtonClick()" & jscript)
    Return b
  End Function

End Class
-------------------------------
クライアント側スクリプト
-------------------------------
<script language="javascript">
  function onMyButtonClick(){
   alert("Button Click !");
  }
</script>

【C#.NETサンプルコード】
-------------------------------
Webフォームクラス
-------------------------------
    private void Page_Load(object sender, System.EventArgs e)
    {
      if (IsPostBack)return;

      MyButtonCell1 bc1=new MyButtonCell1();
      bc1.Text="Button1";
      FpSpread1.Sheets[0].Cells[0,0].CellType=bc1;

      MyButtonCell2 bc2=new MyButtonCell2();
      bc2.Text="Button2";
      bc2.CommandName="I'm btn2";
      FpSpread1.Sheets[0].Cells[1,0].CellType=bc2;

    }
    private void FpSpread1_ButtonCommand(object sender, SpreadCommandEventArgs e)
    {
      //MyButtonCell2で発生するButtonCommandイベントを処理する
      if (e.CommandName=="I'm btn2")
      {
        //クリックされたボタンの位置を取得
        System.Drawing.Point cell= (System.Drawing.Point)e.CommandArgument;
        e.SheetView.Rows[cell.X].ForeColor = Color.Red; //押されたボタンの行で文字色を変更
      }
    }

-------------------------------
MyButtonCell1クラス
-------------------------------
using FarPoint.Web.Spread;

//このボタン型セルはサーバー側にポストバックしません
[Serializable()]public class MyButtonCell1:ButtonCellType
{
  public override Control PaintCell(string id, TableCell parent, Appearance style, Inset margin, object ovalue, bool upperLevel)
  {
    Control c =base.PaintCell(id,parent,style,margin,ovalue,upperLevel);
    Button b=c as Button;
    b.Attributes.Add("onclick","onMyButtonClick()return false");
    return b;
  }

}
-------------------------------
MyButtonCell2クラス
-------------------------------
using FarPoint.Web.Spread;

//このボタン型セルはスクリプトを実行後、サーバー側にポストバックします(FpSpread.ButtonCommandイベントが発生します)
[Serializable()]public class MyButtonCell2:ButtonCellType
{
  public override Control PaintCell(string id, TableCell parent, Appearance style, Inset margin, object ovalue, bool upperLevel)
  {
    Control c =base.PaintCell(id,parent,style,margin,ovalue,upperLevel);
    Button b=c as Button;
    //ボタンに設定されたスクリプトを退避
    string jscript=b.Attributes["onclick"];
    b.Attributes.Add("onclick","onMyButtonClick()"+jscript);
    return b;
  }

}
-------------------------------
クライアント側スクリプト
-------------------------------
VB.NETのサンプルコードで紹介しているものと同様です。
キーワード
クライアント側スクリプト セル型

この文書は、以前は次のFAQ IDで公開されていました : 9144