【セル型】独自の入力検証を行いたい

文書番号 : 25144     文書種別 : 使用方法     最終更新日 : 2007/11/29
文書を印刷する
対象製品
SPREAD for .NET 2.5J Web Forms Edition
詳細
マスク型セルでは正規表現を使用した入力検証を行うことができますが、以下のようにカスタムセル型クラスを作成することで、JavaScriptを使用した入力検証を行うことができます。

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

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

  FpSpread1.Sheets(0).RowCount = 30

  Dim vc1 As MyCustomValidateText1 = New MyCustomValidateText1
  FpSpread1.Sheets(0).Columns(0).CellType = vc1

  Dim vc2 As MyCustomValidateText2 = New MyCustomValidateText2
  FpSpread1.Sheets(0).Columns(1).CellType = vc2
End Sub

-------------------------------
MyButtonCell1クラス
-------------------------------
<Serializable()> Public Class MyCustomValidateText1
  Inherits FarPoint.Web.Spread.GeneralCellType

  ' FpCellType属性に「MyCustomValidateText1」という名前を設定します
  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
    parent.Attributes.Add("FpCellType", "MyCustomValidateText1")
    Return MyBase.PaintCell(id, parent, style, margin, value, upperLevel)
  End Function

End Class

-------------------------------
MyButtonCell2クラス
-------------------------------
<Serializable()> Public Class MyCustomValidateText2
  Inherits FarPoint.Web.Spread.TextCellType
  Implements FarPoint.Web.Spread.Model.ICanSerializeXml

  Private _emsg As String
  Public Property ErrorMessage() As String
    Get
      Return _emsg
    End Get
    Set(ByVal value As String)
      _emsg = value
    End Set
  End Property

  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 t As Control = MyBase.PaintCell(id, parent, style, margin, value, upperLevel)
    If parent.Attributes("FpCellType") IsNot Nothing Then
      parent.Attributes.Remove("FpCellType")
    End If
    parent.Attributes.Add("FpCellType", "MyCustomValidateText2")

    If ErrorMessage IsNot Nothing Then
      parent.Attributes.Add("ErrorMessage", ErrorMessage)
    Else
      parent.Attributes.Add("ErrorMessage", "入力値が不正です") 'ErrorMessageデフォルト
    End If
    Return t
  End Function

  Public ReadOnly Property CanSerializeXml() As Boolean Implements FarPoint.Web.Spread.Model.ICanSerializeXml.CanSerializeXml
    Get
      Return False
    End Get
  End Property
End Class

-------------------------------
クライアント側スクリプト
MyCustomValidateText1クラスで追加したFpCellType属性の
「MyCustomValidateText1」 + 「_isValid」の関数名にします
-------------------------------
<script type="text/javascript">
  function MyCustomValidateText1_isValid(cell, val)
  {
    var tx = document.getElementById("TextBox1");
    // テキストボックスと同じ値が入力されていないとエラー
    if(val == tx.value)
      return "";
    else
      return "エラー";
  }
  function MyCustomValidateText2_isValid(cell, val)
  {
    if(!isNaN(val))
      return "";
    else
      return cell.getAttribute('ErrorMessage');
  }
</script>

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

  FpSpread1.Sheets[0].RowCount = 30;

  MyCustomValidateText1 vc1 = new MyCustomValidateText1();
  FpSpread1.Sheets[0].Columns[0].CellType = vc1;

  MyCustomValidateText2 vc2 = new MyCustomValidateText2();
  FpSpread1.Sheets[0].Columns[1].CellType = vc2;
}

-------------------------------
MyButtonCell1クラス
-------------------------------
[Serializable()]
public class MyCustomValidateText1 : FarPoint.Web.Spread.GeneralCellType
{
  public override System.Web.UI.Control PaintCell(string id, System.Web.UI.WebControls.TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, object value, bool upperLevel)
  {
    parent.Attributes.Add("FpCellType", "MyCustomValidateText1");
    return base.PaintCell(id, parent, style, margin, value, upperLevel);
  }
}

-------------------------------
MyButtonCell1クラス
-------------------------------
[Serializable()]
public class MyCustomValidateText2 : FarPoint.Web.Spread.TextCellType, FarPoint.Web.Spread.Model.ICanSerializeXml
{

  private string _emsg;
  public string ErrorMessage;
  {
    get { return _emsg; }
    set { _emsg = value; }
  }

  public override System.Web.UI.Control PaintCell(string id, System.Web.UI.WebControls.TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, object value, bool upperLevel)
  {

    Control t = base.PaintCell(id, parent, style, margin, value, upperLevel);
    if (parent.Attributes["FpCellType"] != null)
    {
      parent.Attributes.Remove("FpCellType");
    }
    parent.Attributes.Add("FpCellType", "MyCustomValidateText2");

    if (ErrorMessage != null)
    {
      parent.Attributes.Add("ErrorMessage", ErrorMessage);
    }
    else
    {
      parent.Attributes.Add("ErrorMessage", "入力値が不正です");
      //ErrorMessageデフォルト
    }
    return t;
  }

  public bool CanSerializeXml
  {
    get { return false; }
  }
}

-------------------------------
クライアント側スクリプト
-------------------------------
※VBと同じです
キーワード
クライアント側スクリプト セル型

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