saturation
asked on
ASP.NET CustomValidator on checkbox
I'm trying to validate a checkbox with the CustomValidator, but it doesn't seem to be firing. Does it have to do with the fact that I have some regular validator tags on other fields that are firing when the submit button is clicked? They all work just fine--but this one doesn't. There's no message, and no error message, either.
.ASPX PAGE
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator id="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!" OnServerValidate="Validate Checkbox"> * required</asp:CustomValida tor>
CODEBEHIND PAGE
Sub ValidateCheckbox(ByVal source As Object, ByVal args As ServerValidateEventArgs)
args.IsValid = (chkConsent.Checked = True)
End Sub
.ASPX PAGE
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator id="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!" OnServerValidate="Validate
CODEBEHIND PAGE
Sub ValidateCheckbox(ByVal source As Object, ByVal args As ServerValidateEventArgs)
args.IsValid = (chkConsent.Checked = True)
End Sub
Hi,
Try with the following. I had a try with ur code in c# and it worked.
Sub ValidateCheckbox(ByVal source As Object, ByVal args As ServerValidateEventArgs)
If chkConsent.Checked = True Then
args.IsValid = True
Else
args.IsValid = False
End IF
End Sub
<form id="form1" runat="server">
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator ID="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!"
OnServerValidate="Validate Checkbox"> * required</asp:CustomValida tor>
<asp:Button ID="Button1" Text="Submit" runat="server"/>
</form>
Try with the following. I had a try with ur code in c# and it worked.
Sub ValidateCheckbox(ByVal source As Object, ByVal args As ServerValidateEventArgs)
If chkConsent.Checked = True Then
args.IsValid = True
Else
args.IsValid = False
End IF
End Sub
<form id="form1" runat="server">
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator ID="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!"
OnServerValidate="Validate
<asp:Button ID="Button1" Text="Submit" runat="server"/>
</form>
Also the ControlToValidate property is also missing...
ASKER
So, I've noticed one more thing...The checkbox unchecked does prevent the page from being submitted, but there is no message that comes up if it's not checked. Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Make sure when you disable client validation that you need to add if(Page.IsValid) { //validated }
On your event where you want to process the posted data
On your event where you want to process the posted data
write code in form submit event (saving data to DB).
if (!cust_CheckBoxPrint.IsVal id)
{
}
if (!cust_CheckBoxPrint.IsVal
{
}
Does it show the error message if you use
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator id="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!" OnServerValidate="Validate Checkbox" Text="* required" ErrorMessage="* Required" />
?
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator id="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!" OnServerValidate="Validate
?
If so, you could add a piece of javascript in order to validate the checkbox on client-side.