服务器端验证为假时如何防止代码执行 [英] How to prevent execution of code when server side validation false

查看:84
本文介绍了服务器端验证为假时如何防止代码执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插入数据库记录的按钮,还有一个customeValidation函数来检查fromDate和toDate的dateDifference必须少于31天.所以我这样写了custCheckDates_ServerValidate事件.

I have a button to inser records in database and a customeValidation function to check the fromDate and toDate''s dateDifference must be less than 31 days. So I writted custCheckDates_ServerValidate event like this.

DateTime dt1 = new DateTime(year,month, day);
DateTime dt2 = new DateTime(year1, month1, day1);
TimeSpan diffRes = dt2.Subtract(dt1);
if (diffRes.Days > 31)
{
args.IsValid = false;
return;
}
else
args.IsValid = true;


它工作正常,但是当验证为false时,它会显示错误消息,但还会在数据库中插入记录.

因此,如果验证为假,如何停止在数据库中插入记录的执行.我知道,如果我使用ClientSideValidationFunction,然后编写"return false"将其停止,但我希望它在服务器端.


It''s working fine but when validation false it displays error msg but also insert records in the database.

So how I can stop the execution of inserting records in database if validation is false. I know that if I use ClientSideValidationFunction then writting ''return false'' stop it but I want it server side.

推荐答案



您可以使用 Page.IsValid [
Hi,

You can use Page.IsValid[^] property to check if validation succeed. if it returns false then you should not insert your record in database.

So this property will be checked before insert function.

Thanks
-Amit Gajjar


这篇关于服务器端验证为假时如何防止代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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