[英]Allow and Prevent postback in javascript in asp.net
I was trying to validate dates using java script on button click event.If validation returns false post-back should be prevented and if returns true post-back should be allowed.我试图在按钮单击事件上使用 java 脚本验证日期。如果验证返回错误的回发,则应防止回发,如果返回真,则应允许回发。
The problem is that post-back is occuring even if the validation returns false.....问题是即使验证返回 false 也会发生回发.....
I'm new to javascript.Kindly give me any solution.....我是 javascript 的新手。请给我任何解决方案.....
Here's my code这是我的代码
<script type="text/javascript">
function ValidateDate() {
var GridView = document.getElementById('GridView1');
var i = 1;
var rows = GridView.rows.length;
for (var j = 0; j < rows - 2; j++) {
i = i + 1;
var FromDate = new Date(document.getElementById('GridView1_ctl0' + i + '_txtFrom').value);
var ToDate = new Date(document.getElementById('GridView1_ctl0' + i + '_txtTo').value);
if (ToDate < FromDate) {
document.getElementById('GridView1_ctl0' + i + '_txtFrom').focus();
alert('ToDate is less than from date');
return false;
}
}
return true;
}
</script>
I've called javascript on pageload event我在页面加载事件中调用了 javascript
this.btnSave.Attributes.Add("onClick", "ValidateDate();");
You forgot to specify return
你忘了指定
return
You should try with:你应该尝试:
this.btnSave.Attributes.Add("onClick", "return ValidateDate();");
You are returning the value back from the ValidateDate() so for such a condition you need to specify this at the time of function call, and when it returns false then it stops the execution further..您正在从 ValidateDate() 返回值,因此对于这种情况,您需要在 function 调用时指定它,当它返回 false 时,它会进一步停止执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.