I am trying to validate two checkboxes. One of them must be checked in order for the form to be valid. I would like to use a CustomValidator control, and validate on the server.
(This .ascx page is a form that is displayed on a different .aspx page.)
First I put in the checkboxes and a CustomValidator control on my .ascx page. Like this:
<tr>
<td colspan="3">
<input type="checkbox" runat="server" name="EmailCourse" class="" id="EmailCourse" value="" />
Email course
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="No checkbox checked"
OnServerValidate="validateCheckBoxes_ServerValidate">
</asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="3">
<input type="checkbox" runat="server" name="SpecialReport" class="" id="SpecialReport" value="" />
Special report
</td>
</tr>
Then, I added the validateCheckBoxes_ServerValidate function in the code-behind, on the .ascx.cs page, like this:
protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
{
if (!EmailCourse.Checked && !SpecialReport.Checked)
args.IsValid = false;
else
args.IsValid = true;
}
When I try to open the page that uses this form on my local site to see what it looks like, I get an error, like this:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.common_controls_specialreportform_ascx' does not contain a definition for 'validateCheckBoxes_ServerValidate' and no extension method 'validateCheckBoxes_ServerValidate' accepting a first argument of type 'ASP.common_controls_specialreportform_ascx' could be found (are you missing a using directive or an assembly reference?)
And:
error CS1061: 'ASP.common_controls_specialreportform_ascx' does not contain a definition for 'validateCheckBoxes_ServerValidate' and no extension method 'validateCheckBoxes_ServerValidate' accepting a first argument of type 'ASP.common_controls_specialreportform_ascx' could be found (are you missing a using directive or an assembly reference?)
Does anyone know what the cause of this error is? I'm new at asp.net and am having trouble with this.
Thanks!
You put validateCheckBoxes_ServerValidate in *.ascx.cs when it should be on ur aspx.cs . On ascx.cs you can't refer the control it is on Parent like this.
put this code to ur aspx.cs file:
protected void validateCheckBoxes_ServerValidate(object source, ServerValidateEventArgs args)
{
if (!EmailCourse.Checked && !SpecialReport.Checked)
args.IsValid = false;
else
args.IsValid = true;
}
Edit:
Your custom Validator on ascx should seems like:
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="No checkbox checked" ControlToValidate="EmailCourse" OnServerValidate="validateCheckBoxes_ServerValidate"/>
without this ControlToValidate attribute server don't know which control u would like to validate.
Edit2:
Did u try to use change <input type="checkbox"/> to <asp:CheckBox />?
and tell me how this should be Validate after btn click or after checkbox checked/unchecked?
Edit3:
Check that in your .ascx.designer.cs EmailCourse got proper Type.
Edit4:
When u have <asp:CheckBox .../> on your *.ascx file u should have in your ascx.designer.cs
this type protected global::System.Web.UI.WebControls.CheckBox EmailCourse
Please let me know if this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With