代わりに、「ValidateMatrixFunc」プロパティをイベントとして公開することをお勧めします。なんで?コントロールが通常どのように実装されるかにより一貫性があります。また、イベントを使用すると、1 つのイベントに対して複数のサブスクライバー (イベント ハンドラー) を持つことができます。これは典型的な使用例ではないかもしれませんが、時々発生します。
これをイベントとして実装する方法を以下に説明しました。
イベントを「ValidatingMatrix」と呼びましょう。
次に、次のように ASPX マークアップを記述できます。
<uc1:MatrixTable ID="ucMatrixTable" runat="server" OnValidatingMatrix="ValidateMatrix" />
CancelEventHandler
また、代わりにデリゲートを使用しましょうFunc<bool>
。これは、コード ビハインドの ValidateMatrix メソッド シグネチャが次のようになる必要があることを意味します。
protected void ValidateMatrix(object sender, System.ComponentModel.CancelEventArgs e)
{
// perform validation logic
if (validationFailed)
{
e.Cancel = true;
}
}
MatrixTable カスタム コントロール内で、次のようなものを実装します。
const string ValidatingMatrixEventKey = "ValidatingMatrix";
public event System.ComponentModel.CancelEventHandler ValidatingMatrix
{
add { this.Events.AddHandler(ValidatingMatrixEventKey, value); }
remove { this.Events.RemoveHandler(ValidatingMatrixEventKey, value); }
}
protected bool OnValidatingMatrix()
{
var handler = this.Events[ValidatingMatrixEventKey] as System.ComponentModel.CancelEventHandler;
if (handler != null)
{
// prepare event args
var e = new System.ComponentModel.CancelEventArgs(false);
// call the event handlers (an event can have multiple event handlers)
handler(this, e);
// if any event handler changed the Cancel property to true, then validation failed (return false)
return !e.Cancel;
}
// there were no event handlers, so validation passes by default (return true)
return true;
}
private void MyLogic()
{
if (this.OnValidatingMatrix())
{
// validation passed
}
else
{
// validation failed
}
}