【セル型】コンボボックス型セルで選択された要素のValues値を 取得する(クライアント側で)

文書番号 : 21874     文書種別 : 使用方法     最終更新日 : 2006/06/23
文書を印刷する
対象製品
SPREAD for .NET 2.5J Web Forms Edition
詳細
コンボボックスのValue値を取得する方法は、サーバー側で処理する方法とクライアント側で処理する方法の2種類あります。ここでは、クライアント側で処理する方法をShowButtonプロパティがTrue(コンボボックスボタンを常に表示)の場合、False(コンボボックスボタンがアクティブセルの場合のみ表示)の場合に分けて説明します。

サーバー側で処理する実装方法については以下の「関連するFAQ」をご参照ください。ただし、サーバー側で処理する場合にはポストバックが必要となります。

ComboBoxCellTypeクラスではコンボボックスのText(表示する文字列)とValue(値)の関連付けをサーバー側にて行っています。そのため、Webフォームに非表示のDropDownListコントロールを追加し、Values値を格納する必要があります。

下記サンプルコードでは、ShowButtonプロパティをTrueにした場合とFalseにした場合のコンボボックスのValue値を隣のセルに表示する方法を記載しています。

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

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

    '2列目にコンボボックス型セルを設定
    Dim cb As New MyCombo()
    cb.Items = New String() {"", "A", "B", "C"}
    cb.Values = New String() {"", "aaa", "bbb", "ccc"}
    'ShowButtonプロパティをTrueに設定
    cb.ShowButton = True
    FpSpread1.ActiveSheetView.Columns(1).CellType = cb
    'Values要素をDropDownListに設定
    DropDownList1.Items.AddRange(New ListItem() {New ListItem(""), New ListItem("aaa"), New ListItem("bbb"), New ListItem("ccc")})
    'DropDownListを非表示
    DropDownList1.Style.Add("visibility", "hidden")
  End Sub

----------------------------------------
カスタムコンボ型(MyComboクラス)
※ShowButtonプロパティをTrueに設定した場合
----------------------------------------
<Serializable()> Public Class MyCombo
  Inherits FarPoint.Web.Spread.ComboBoxCellType

  Public Overrides Function PaintCell(ByVal id As String, ByVal parent As 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
    'onchangeイベントにクライアント側スクリプトを関連付け
    Dim dlist As DropDownList = CType(MyBase.PaintCell(id, parent, style, margin, value, upperLevel), DropDownList)
    dlist.Attributes.Add("onchange", "dchange()")
    Return dlist
  End Function

End Class

----------------------------------------
カスタムコンボ型(MyComboクラス)
※ShowButtonプロパティをFalseに設定した場合
----------------------------------------
<Serializable()> Public Class MyCombo
  Inherits FarPoint.Web.Spread.ComboBoxCellType

  Public Overrides Function GetEditorControl(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
    'onchangeイベントにクライアント側スクリプトを関連付け
    Dim dlist As DropDownList = CType(MyBase.GetEditorControl(id, parent, style, margin, value, upperLevel), DropDownList)
    dlist.Attributes.Add("onchange", "dchange()")
    Return dlist
  End Function

End Class

----------------------------------------
クライアント側スクリプト
WebForm1.aspxのHTMLソースに追加します。
----------------------------------------
<script language="javascript">
function dchange(){
  var dlist=event.srcElement;
  var templist=document.all("DropDownList1");
  var val=(templist.options[dlist.selectedIndex]).value;
  var s=document.all("FpSpread1");
  //隣のセルにValue値を設定
  s.SetValue(s.ActiveRow,s.ActiveCol+1,val,true);
}
</script>


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

      //2列目にコンボボックス型セルを設定
      MyCombo1 cb=new MyCombo1();
      cb.Items=new string[]{"","A","B","C"};
      cb.Values=new string[]{"","aaa","bbb","ccc"};
      //ShowButtonプロパティをTrueに設定
      cb.ShowButton=true;
      FpSpread1.ActiveSheetView.Columns[1].CellType=cb;

      //Values要素をDropDownListに設定
      DropDownList1.Items.AddRange(new ListItem[]{new ListItem(""), new ListItem("aaa"), new ListItem("bbb"), new ListItem("ccc")});
      //DropDownListを非表示
      DropDownList1.Style.Add("visibility", "hidden");

    }

----------------------------------------
カスタムコンボ型(MyComboクラス)
※ShowButtonプロパティをTrueに設定した場合
----------------------------------------
  [Serializable()]public class MyCombo:FarPoint.Web.Spread.ComboBoxCellType
  {
    public override System.Web.UI.Control PaintCell(string id, TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, Object ovalue, Boolean upperLevel)
    {
      DropDownList dlist= base.PaintCell(id,parent,style,margin,ovalue,upperLevel) as DropDownList;
      dlist.Attributes.Add("onchange", "dchange()");
      return dlist;
    }
  }

----------------------------------------
カスタムコンボ型(MyComboクラス)
※ShowButtonプロパティをFalseに設定した場合
----------------------------------------
  [Serializable()]public class MyCombo:FarPoint.Web.Spread.ComboBoxCellType
  {
    public override System.Web.UI.Control GetEditorControl(string id, TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, Object ovalue, Boolean upperLevel)
    {
      DropDownList dlist= base.GetEditorControl(id,parent,style,margin,ovalue,upperLevel) as DropDownList;
      dlist.Attributes.Add("onchange", "dchange()");
      return dlist;
    }
  }

----------------------------------------
クライアント側スクリプト
WebForm1.aspxのHTMLソースに追加します。
----------------------------------------
VB.NETサンプルコードと同様です。
関連情報
キーワード
クライアント側スクリプト セル型

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