0

カスタム確認メッセージ ボックス コントロールを作成し、次のようなイベントを作成しました。

[Category("Action")]
[Description("Raised when the user clicks the button(ok)")]
    public event EventHandler Submit;

protected virtual void OnSubmit(EventArgs e) {
     if (Submit != null)
        Submit(this, e);
}


イベント OnSubmit は、ユーザーが確認ボックスの [OK] ボタンをクリックすると発生します。

void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
    OnSubmit(e);
}


今、私はこのOnSubmitイベントをこのように動的に追加しています
-In aspx-

<my:ConfirmMessageBox ID="cfmTest" runat="server" ></my:ConfirmMessageBox>
    <asp:Button ID="btnCallMsg" runat="server" onclick="btnCallMsg_Click" />
    <asp:TextBox ID="txtResult" runat="server" ></asp:TextBox>

cs-で

protected void btnCallMsg_Click(object sender, EventArgs e)
{
  cfmTest.Submit += cfmTest_Submit;//Dynamically Add Event
  cfmTest.ShowConfirm("Are you sure to Save Data?");  //Show Confirm Message using Custom Control Message Box
}

    protected void cfmTest_Submit(object sender, EventArgs e)
        {
          //..Some Code..
          //..
          txtResult.Text = "User Confirmed";//I set the text to "User Confrimed" but it's not displayed
          txtResult.Focus();//I focus the textbox but I got Error
        }

私が得たエラーは
、System.InvalidOperationException was unhandled by user code Message="SetFocus can only call before and during PreRender." です。ソース="System.Web"

そのため、カスタム コントロールのイベントを動的に追加して起動すると、Web コントロールでエラーが発生します。このようにaspxファイルにイベントを追加すると、

<my:ConfirmMessageBox ID="cfmTest" runat="server" OnSubmit="cfmTest_Submit"></my:ConfirmMessageBox>

エラーは発生せず、正常に動作します。

イベントをカスタム コントロールに動的に追加するのを手伝ってくれる人はいますか?
ありがとう。

4

2 に答える 2

1

問題は、ライフサイクルの後半に追加されるイベントと、イベントハンドラーで達成しようとしているものの組み合わせにはありません。

エラーが明確に示すように、問題は次の行にあります。

txtResult.Focus();

コントロールにフォーカスを設定できるようにする場合は、イベント ハンドラーをInitまたはに追加する必要がありますLoad

jquery を使用してクライアント側でフォーカスを設定することで、この問題を回避できます。

var script = "$('#"+txtResult.ClientID+"').focus();";

RegisterClientScriptBlockを使用してこれを発行する必要があります。

于 2012-08-29T03:50:18.493 に答える
0

最も簡単な変更は、focus() 呼び出しを移動することです。

bool focusResults = false;

    protected void cfmTest_Sumit(object sender, EventArgs e)
    {
      txtResult.Text = "User Confirmed";
     focusResults = true;
    }

    protected override void OnPreRender(EventArgs e)
    {
       base.OnPreRender(e);

        if(focusResults)
           txtResult.Focus();
    }

txtResult.Text が別の場所に再設定されていないことを確認しますか?

于 2012-08-29T03:47:25.967 に答える