ASP.Net OnLoad停止事件处理
我的WebForm页面中有一些验证代码。当用户单击按钮并进行回发时 处理Page_Load事件,然后处理Button1_Click事件 如果页面加载验证失败,我无法找到停止Button1\u Click事件处理的方法 这是一个诡计吗ASP.Net OnLoad停止事件处理,asp.net,postback,pageload,Asp.net,Postback,Pageload,我的WebForm页面中有一些验证代码。当用户单击按钮并进行回发时 处理Page_Load事件,然后处理Button1_Click事件 如果页面加载验证失败,我无法找到停止Button1\u Click事件处理的方法 这是一个诡计吗 谢谢,下面显示了4个变体 public partial class _Default : System.Web.UI.Page { private bool MyPageIsValid { get; set; } prot
谢谢,下面显示了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_中检查它,单击:
if (!this.isValid) { return; }
你的代码是什么样子的?它是一个简单的页面加载和按钮点击。这是一个简单的页面加载和按钮点击受保护的无效页面加载(对象发送方,事件参数e){if(HasError)返回;}受保护的无效btnTest\u单击(对象发送方,事件参数e){//按钮单击}受保护的无效页面加载(对象发送方,事件参数e){if(HasError)返回;}受保护的无效btnTest\u单击(对象发送方,事件参数e){//按钮单击}