【セル型】 ラジオボタンのグループを列に対して設定したい

文書番号 : 35468     文書種別 : 使用方法     登録日 : 2013/03/01     最終更新日 : 2014/10/21
文書を印刷する
対象製品
SPREAD for ASP.NET 7.0J
詳細
メモメモ
  • 既存のセル型を継承しカスタムセルを作成する場合、不具合修正や内部仕様の変更により動作が変わる可能性があります。

製品のラジオボタンリスト型セルはセルに対してグループを設定します。そのため1つのセルの中にラジオボタンが幾つか配置され、セルの中で排他的にこれらをチェックできるという動作になります。

ただ、アプリケーションでは列内の各セルに1つずつラジオボタンを配置し、列内で排他的にチェックできるようにしたいことがあります。

これはRadioButtonListCellTypeクラスを継承する独自のカスタムセル型クラスを作成することで実現できます。
下記はこの様な独自のカスタムセル型クラスのサンプルコードです。

【カスタムラジオボタン型の注意点】
1.このセル型は列に対して設定してください。
2.Itemsで指定するリストの選択項目の個数は行数に合わせてください。
3.ラジオボタン選択の有無はページのポストバック時に検出できます。

◎サンプルコード(VB)
-------------------------------
Webフォームクラス
-------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Dim rcelltype As New myRadioCellType
  Dim i As String
  Dim j As Integer

  If IsPostBack Then
    ' ポストバック時にラジオボタン選択の有無を調べます
    i = Page.Request.Form("radiobtn")
    If Not (i Is Nothing) Then
      ' チェックされたいるボタンのみ、セルの値を1に設定
      For j = 0 To FpSpread1.Sheets(0).RowCount - 1
        FpSpread1.Sheets(0).Cells(j, 1).Value = 0
      Next
      FpSpread1.Sheets(0).Cells(CInt(i), 1).Value = 1
    End If
    Return
  End If

  FpSpread1.Sheets(0).RowCount = 3

  rcelltype.Items = New String() {"up", "side", "down"}
  FpSpread1.Sheets(0).Columns(1).CellType = rcelltype
  FpSpread1.Sheets(0).Columns(1).Locked = True
End Sub

-------------------------------
myRadioCellTypeクラス
-------------------------------
<Serializable()> Public Class myRadioCellType
  Inherits FarPoint.Web.Spread.RadioButtonListCellType

  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 row As String
    Dim myid As String
    Dim idarray As String() = id.Split(",".ToCharArray())

    myid = idarray(0)
    row = CStr(id.Split(",".ToCharArray()).GetValue(0))
    Dim c As RadioButton = New MyBtn(Items(CInt(row)))
    c.ID = myid
    If CStr(value) = "1" Then
      c.Checked = True
    End If
    Return c

  End Function

  Public Overrides Function Format(ByVal o As Object) As String
    If Not o Is Nothing Then
      Return o.ToString
    Else
    Return MyBase.Format(o)
    End If
  End Function

End Class

-------------------------------
MyBtnクラス
-------------------------------
<Serializable()> Public Class MyBtn
  Inherits RadioButton
  Dim radiotext As String

  Public Sub New()

  End Sub

  Public Sub New(ByVal text As String)
    radiotext = text
  End Sub

  Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    If Me.Checked = True Then
      writer.WriteLine("<span><input id=""gc"" type=""radio"" CHECKED name=""radiobtn""value=""" & Me.ID & """ />" & radiotext & "</span>")
    Else
      writer.WriteLine("<span><input id=""gc"" type=""radio"" name=""radiobtn""value=""" & Me.ID & """ />" & radiotext & "</span>")
    End If
  End Sub

End Class

◎サンプルコード(C#)
-------------------------------
Webフォームクラス
-------------------------------
protected void Page_Load(object sender, EventArgs e)
{
  myRadioCellType rcelltype = new myRadioCellType()
  string i

  if (IsPostBack)
  {
    // ポストバック時にラジオボタン選択の有無を調べます
    i = Request.Form["radiobtn"]
    if (i != null)
    {
      // チェックされたいるボタンのみ、セルの値を1に設定
      for (int j = 0 j < FpSpread1.Sheets[0].RowCount j++)
      {
        FpSpread1.Sheets[0].Cells[j, 1].Value = 0
      }
      FpSpread1.Sheets[0].Cells[Int32.Parse(i), 1].Value = 1
    }
    return
  }

  FpSpread1.Sheets[0].RowCount = 3
  rcelltype.Items = new String[] { "up", "side", "down" }
  FpSpread1.Sheets[0].Columns[1].CellType = rcelltype
  FpSpread1.Sheets[0].Columns[1].Locked = true
}

-------------------------------
myRadioCellTypeクラス
-------------------------------
[Serializable()] public class myRadioCellType : FarPoint.Web.Spread.RadioButtonListCellType
{
  public override Control PaintCell(string id, TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, object ovalue, bool upperLevel)
  {
    string row
    string myid
    string[] idarray = id.Split(',')

    myid = idarray[0]
    row = (string)id.Split(',').GetValue(0)
    RadioButton c = new MyBtn(Items[Int32.Parse(row)])
    c.ID = myid

    if (ovalue != null && ovalue.ToString() == "1")
      c.Checked = true

    return c
  }

  public override string Format(object o)
  {
    if (o != null)
      return o.ToString()
    return null
  }
}

-------------------------------
RadioButtonクラス
-------------------------------
[Serializable()] public class MyBtn : RadioButton
{
  string radiotext

  public MyBtn() : base() { }
  public MyBtn(string text) { radiotext = text }

  protected override void Render(HtmlTextWriter writer)
  {
    if (this.Checked == true)
    {
      writer.WriteLine("<span><input id="gc" type="radio" CHECKED name="radiobtn"value="" + this.ID + "" />" + radiotext + "</span>")
    }
    else
    {
      writer.WriteLine("<span><input id="gc" type="radio" name="radiobtn"value="" + this.ID + "" />" + radiotext + "</span>")
    }
  }
}