How could I validate the html checkbox in asp.net?
I've tried the following.
<input type="checkbox" id="chk_rule" class="checkbox-custom" name="chk_rule" runat="server"/>
<label for="bodycontent_chk_rule" class="checkbox-custom-label">I have read and agree to the official rules</label>
<asp:CustomValidator ID="Agreecheck" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="You have to agree the rules." ControlToValidate="chk_rule" OnServerValidate="Agreecheck_ServerValidate"></asp:CustomValidator>
And the server-side validation function is following.
protected void Agreecheck_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = chk_rule.Checked;
}
But I've got the following error.
Message: Unable to find control id 'chk_rule' referenced by the 'ControlToValidate' property of 'Agreecheck'. Stack Trace: at System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) at System.Web.UI.WebControls.CustomValidator.ControlPropertiesValid() at System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
How could I fix this?