我有两个文本框,分别是 to_date 和 from_date。我比较两个文本框并获取值,但它的执行紧接着我想要停止的按钮的单击,因为如果 to_date 小于起始日期,则截止日期将变为空,并且重点关注文本框不会转到的单击事件按钮。
我使用:
try
{
DateTime from = DateTime.ParseExact(txt_from_date.Text, "M/d/yyyy", theCultureInfo);
DateTime to = DateTime.ParseExact(txt_to_date.Text, "M/d/yyyy", theCultureInfo);
int result = DateTime.Compare(to, from);
e.IsValid = result>0;
if (e.IsValid == false)
{
txt_to_date.Text = "";
txt_to_date.Focus();
}
}
catch (Exception eq)
{
e.IsValid = false;
txt_to_date.Text = "";
}
最佳答案
如果您的 onclick 事件处理程序,您必须调用验证:
protected void YourButton_OnClick(object sender, EventArgs e)
{
Page.Validate();
if(Page.IsValid) // Will be false if any validator is invalid
{
// your code here
}
}
请注意,如果您必须将表单拆分为多个子部分,则可以在验证控件上设置 ValidationGroup
并将相同的值传递给 Page.Validate
方法。
关于c# - 如果为 false,则停止在自定义验证器中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11220312/