Validation Controls in User Control not stopping page postback
I have a asp button and a user control containing custom validator in it when i click the button that is present on the page not inside the usercontrol, the user control client side validation occurs and displays the javascript method regarding data entry failure but when i click the javascript alert box ok button, the postback gets occured automatically i want to stop the postback if the user control contains invalid data in iother words the postback get occured only when the user control contains the valid data.
Any suggestions will be appreciated.
I have already done this type of coding in javascript:
function CheckTimeRangeTo_9(sender, args) {
var e = document.getElementById('<%= ddTimeTableTo_9.ClientID %>');
var totalToTimeInMins = CalculateTotalMinutes(e.options[e.selectedIndex].value);
var totalFromTimeInMins = CalculateTotalMinutes(args.Value);
if (totalToTimeInMins != 0 &开发者_运维百科;& totalToTimeInMins < totalFromTimeInMins) {
alert("From time cannot be greater than to time in time slice ID 9.");
args.IsValid = false;
return;
}
args.IsValid = true;
}
It seems you shouldn't return anything here. I mean return;
in function CheckTimeRangeTo_9 shouldn't be here. And args.IsValid = true;
should be inside else statement. So, you should try:
function CheckTimeRangeTo_9(sender, args) {
var e = document.getElementById('<%= ddTimeTableTo_9.ClientID %>');
var totalToTimeInMins = CalculateTotalMinutes(e.options[e.selectedIndex].value);
var totalFromTimeInMins = CalculateTotalMinutes(args.Value);
if (totalToTimeInMins != 0 && totalToTimeInMins < totalFromTimeInMins) {
alert("From time cannot be greater than to time in time slice ID 9.");
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
Do you set IsValid
property of the parameter which is passed to your client-side validation function?
<script type="text/javascript">
function myValidator_ClientValid(sender, args) {
var valid = false;
// Perform validation
args.IsValid = valid;
}
</script>
精彩评论