JavaScript时在ASP.NET中禁用如何处理验证 [英] How to handle validation when JavaScript is disabled in ASP.NET
问题描述
我使用了一些典型的ASP.NET的验证控件在我的网站。现在,我试图禁用的JavaScript
在我的浏览器来测试我的应用程序,当然还有验证控件
不再作品。我认为这是最好的尝试,以使他们的工作使用建议的解决方案之一,到这里,而不是再投资的车轮和页面建立一个验证层或我的对象的 -Am我想正确的? - 的
I'm using some of the typical ASP.NET's Validation Controls in my website. Now I'm trying to disable the JavaScript
in my browser to test my application and of course the Validation Controls
no longer works. I think it's best to try to make them work using one of the suggested solutions down here instead of reinvesting the wheel and build a validation layer for the page or my objects -Am I thinking right?-
你觉得这些选项和原因:
What do you think of these options and why:
-
在包含点击按钮的事件code,以检查页面是否有效,如果没有显式调用
Page.Validate();
方法
检查是否启用JavaScript,如果不是我应该叫 Page.Validate();
Check if whether the JavaScript is enabled and if not I should call Page.Validate();
如果你有更好的办法做到这一点,请让我知道。
If you there's a better way to do it please let me know.
推荐答案
验证控件旨在验证的主要的服务器端。客户端验证是可选的(请参阅 EnableClientScript
属性)。所以,如果他们不使用JavaScript禁用,那么你很可能缺少在你的页面,一个小样板code如的上Page.IsValid MSDN文档:
The validation controls are designed to validate primarily on the server side. The client-side validation is optional (see the EnableClientScript
property). So if they aren't working with Javascript disabled, then you're probably missing a little boilerplate code in your page, such as this snippet from the MSDN documentation on Page.IsValid:
private void ValidateBtn_Click(Object Sender, EventArgs E)
{
Page.Validate();
if (Page.IsValid == true) // yes, it is written this way in the MSDN documentation
lblOutput.Text = "Page is Valid!";
else
lblOutput.Text = "Some required fields are empty.";
}
您也可以拨打 Page.Validate
和检查 Page.IsValid
在你的页面的的OnLoad
事件,这样就可以从出发到下一步prevent回发时,窗体需要重新提交。你可能甚至不需要调用验证()
明确 - Button.CausesValidation
缺省为true
You can also call Page.Validate
and check Page.IsValid
in your Page's OnLoad
event, so that you can prevent a postback from proceeding to the next step when the form needs to be re-submitted. You probably don't even need to call Validate()
explicitly — Button.CausesValidation
is true by default.
这篇关于JavaScript时在ASP.NET中禁用如何处理验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!