如果为false,则停止在Custom Validator中执行 [英] stop execution in Custom validator if it false

查看:230
本文介绍了如果为false,则停止在Custom Validator中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,分别为to_date和from_date.并且我比较两个文本框并获取值,但是它执行到我要停止的按钮的onclick旁边,因为如果to_date小于from日期,则to date变为null,并且专注于该文本框不会转到click事件按钮.

i have two textboxes as to_date and from_date. and i compare both textboxes and get value but it execution goes next to onclick of button that i want to stop because if to_date is less than from date, the to date is become null as well as focus on that textboxes not goes to click event of button.

我使用:

 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事件处理程序必须调用验证:

If your onclick event handler, you have to call the validation :

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 方法.

Note that you can set the ValidationGroup on validation controls and pass the same value to Page.Validate method, if you have to split your form in several sub sections.

这篇关于如果为false,则停止在Custom Validator中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆