1

我有一个文本框和一个提交按钮。文本框是一个日期输入字段。

附加到它,我有一个类型设置为“日期”的比较验证器。它确实验证并显示错误消息。

问题是,用户仍然可以点击提交按钮。我想防止这种情况发生。如果用户输入了 03/hello/2011 之类的内容,他或她应该无法提交表单。

我怎样才能做到这一点?

有任何想法吗?

谢谢,

杰森

4

2 回答 2

5

将验证器和提交按钮关联到一个验证组中。他们都有属性验证组。为控件的属性提供一个名称,例如 pageValidation。

于 2011-06-28T17:54:53.353 回答
0

我自己也遇到过这个问题,当用户单击提交按钮时,带有验证错误的页面仍然可以继续提交。

你可以做的是这样的:

    protected void submitClicked(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
           // somehow the user was able to submit their form even though there are
           // validation errors. Stop here and let ASP.NET present the error messages
           // to the user
           return; 
        }

        // do submission stuff here like putting things in the database
    }
于 2011-06-28T17:56:13.837 回答