私はすべてのボタンの基本カスタムページにイベントを登録するという考えがありました。私はそれが保存または編集であったかどうかを確認し、承認と検証を行うことができます。派生したページは、そのページに保存するための詳細を行うためにclickイベントを処理します。どのイベントが最初に起きるかはわかりませんでした。親ページが最初に実行される場合、IsAuthorizedまたはIsValidatedプロパティを設定し、派生した各ページのクリックハンドラでそれを確認することで、期待される動作を得ることができます。C#で特定の順序でイベントを処理する方法を教えてください。
とにかく、基本ページのイベントハンドラを実行して最初に完了させる方法はありますか、これを救済する方法はありますか?
EDIT:アプリケーションのすべての編集/保存/更新ボタンハンドラにボイラープレートコードを追加する必要がない、シンプルなデザインを見落としていますか?そして、あるイベントハンドラが別のイベントハンドラと通信する正しい方法は何ですか?たとえば、基本ページが成功または失敗の検証を伝達する必要がある場合、派生クラスは保存の成功または失敗を伝える必要があるため、監査に記録することができます。
//Base page-- seems like this will work if events happen in order of wireup.
protected override void OnInit(EventArgs e)
{
foreach (Control possibleButton in Controls)
{
if(possibleButton is Button)
{
Button button = (Button) possibleButton;
button.Command += ButtonCommandPreconditions;
}
}
base.OnInit(e);
foreach (Control possibleButton in Controls)
{
if(possibleButton is Button)
{
Button button = (Button) possibleButton;
button.Command += ButtonCommandPostconditions;
}
}
}
void ButtonCommandPreconditions(object sender, CommandEventArgs e)
{
if(e.CommandName=="Save" || e.CommandName=="Edit")
{
//Stuff that needs to happen before other handler
//Validate, display failures-- maybe set IsValdated property
//Check for POST/GET, throw exception on GET.
//Check for ID, throw exception on anonymous user
//Check for authorization
//Display authorization failures-- maybe set IsAuthorized property
}
}
void ButtonCommandPostconditions(object sender, CommandEventArgs e)
{
if(e.CommandName=="Save" || e.CommandName=="Edit")
{
//Stuff that needs to happen *after* other handler
//Log save
}
}
編集:利きがwireupの順に処理されることになっているそのイベントを反映する 変更されたコード。
ああ!これは私の基本的な考え方に欠陥があることを私に確信させる。ありがとう!それは私に盲目的な路地を下る多くの努力を救った。 – MatthewMartin
あなたは他人からそれを聞く必要があると思いますか? – womp