[英]ASP.Net OnLoad Stop event handling
我的WebForm頁面中有一些驗證代碼。 用戶單擊按鈕並回發時。
Page_Load事件得到處理,然后Button1_Click事件得到處理。
如果驗證在Page_Load上失敗,我想不出一種方法來停止處理Button1_Click事件。
這樣做有技巧嗎?
謝謝
如下所示有4種變化。
public partial class _Default : System.Web.UI.Page
{
private bool MyPageIsValid { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
bool valid = false; /* some routine here */
MyPageIsValid = valid;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.MyPageIsValid)
{
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (!this.MyPageIsValid) {return;}
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
protected void Button3_Click(object sender, EventArgs e)
{
if (this.Page.IsValid)
{
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
if (!this.Page.IsValid) {return;}
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
}
我認為最好像這樣在Button1_Click方法中准確檢查驗證條件:
if (!this.IsValid()) { return; }
另外,如果您仍然想檢查Page_Load方法中的條件,只需將簡單的'bool isValid'標志添加到頁面的類中,然后在Button1_Click中進行檢查:
if (!this.isValid) { return; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.