我創建了一個自定義的cofirm消息框控件自定義控件(確認消息框),我喜歡創造這 -動態添加事件
[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);
}
當用戶點擊OK按鈕事件的OnSubmit事件發生在Confrim Box上。
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnSubmit(e);
}
現在我加入這個onsubmit事件動態喜歡這個 -
在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是未處理的d用戶代碼 消息=「SetFocus只能在PreRender之前和之前調用。」 來源=「的System.Web」
所以,當我動態地添加和消防自定義控件的事件,有在網絡控制錯誤。 如果我喜歡這個aspx文件添加事件,
<my:ConfirmMessageBox ID="cfmTest" runat="server" OnSubmit="cfmTest_Submit"></my:ConfirmMessageBox>
沒有錯誤,做工精細。
任何人都可以幫助我動態添加事件到自定義控件嗎?
謝謝。
如果我在aspx文件中添加了事件(不是動態的),它正確顯示txtResult中的文本。 – nnnn
將它添加到aspx時,這些事件處理程序在創建控件時添加,通常在'Init'上。 – nunespascal