简体   繁体   English

ASP.NET验证服务器端

[英]ASP.NET Validation server side

I have an ASP.NET webform which I want to validate Client-Side and Server-Side, using the same validation controls. 我有一个ASP.NET webform,我想使用相同的验证控件来验证客户端和服务器端。 I can't seem to find the solution for this - the client validation works great, but when I disable javascript - It ignores the validation. 我似乎无法找到解决方案 - 客户端验证工作得很好,但是当我禁用javascript时 - 它忽略了验证。

Help would much be appreciated. 非常感谢帮助。

Roman 罗马

Explicitly call Page.Validate() on the server side. 在服务器端显式调用Page.Validate()

Or the overloaded Page.Validate(string) to target one of your validation groups. 或者重载的Page.Validate(字符串)来定位您的一个验证组。

Update: 更新:

I forgot, after you run Validate(..), check the Page.IsValid property - it's up to you to stop the page from submitting if this property == false . 我忘记了,运行验证(..)后,检查Page.IsValid属性 - 如果此属性== false则由您决定是否停止提交页面。

You can always trigger validation by the validator1.Validate() method, which will do the server-side comparison. 您始终可以通过validator1.Validate()方法触发验证,该方法将执行服务器端比较。 Check Page.IsValid to see if server-side validation isn't being performed? 检查Page.IsValid以查看是否未执行服务器端验证? I think you can invoke it via Page.Validate() . 我想你可以通过Page.Validate()调用它。

HTH HTH

If you're using standard validation controls, data is always re-verified on the server even if client side validation is specified. 如果您正在使用标准验证控件,即使指定了客户端验证,也始终在服务器上重新验证数据。

See the note in this article right after figure 2:, which says: 请参阅图2中的本文中的注释,其中说:

Double-Checking Client-Side Validation 双重检查客户端验证

One interesting point is that even though the form data is validated on the client (eliminating the need for additional requests to and responses from the server to validate the data), the data entered is re-verified on the server. 一个有趣的观点是,即使表单数据在客户端上得到验证(消除了对服务器的额外请求和响应以验证数据的需要),输入的数据也会在服务器上重新验证。 After the data is checked on the client and found valid, it is rechecked on the server using the same validation rules. 在客户端上检查数据并发现有效后,将使用相同的验证规则在服务器上重新检查数据。 These are rules that you establish to ensure against some tricky programmer out there trying to bypass the validation process by posting the page to the server as if it passed validation. 这些是你建立的规则,以确保一些棘手的程序员试图通过将页面发布到服务器来绕过验证过程,就像它通过验证一样。

http://msdn.microsoft.com/en-us/library/aa479013.aspx http://msdn.microsoft.com/en-us/library/aa479013.aspx

However, you can force validation on the server by calling Page.Validate() 但是,您可以通过调用Page.Validate()强制在服务器上进行验证

Roman, 罗马,

You can use the ASP.net custom validator to provide both a client and a server method for validation. 您可以使用ASP.net自定义验证程序提供客户端和服务器方法进行验证。 That way if you disable the js you should still hit the server validation method. 这样,如果你禁用js,你仍然应该点击服务器验证方法。 In this example the "ClientValidate" function would be defined in a javascript block on your page, and teh "ServerValidate" function would exist in your codebehind file. 在此示例中,“ClientValidate”函数将在页面上的javascript块中定义,并且“ServerValidate”函数将存在于您的代码隐藏文件中。

<asp:textbox id="textbox1" runat="server">
<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage="*This box is not valid" dispaly="dynamic">*
</asp:CustomValidator>

Found the answer! 找到答案了! The answer is to use Page.Validate() and then check for Page.IsValid to check if the validation was valid or not. 答案是使用Page.Validate()然后检查Page.IsValid以检查验证是否有效。 Only using Page.Validate() didn't help - the code proceeded and didn't stop. 只使用Page.Validate()没有帮助 - 代码继续并且没有停止。

Thanks guys, Roman 谢谢你们,罗曼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM