When building out an Asp.Net web application, very often you need to require that the user check a box saying they agree to the "Terms and Conditions" of the website. Within the Asp.Net framework, there are many validators but non that inherently validate a checkbox control. After doing research on this issue I found a few ways to handle the issue. The first would be to build a custom validator that would take care of the issue. This would be a good option for the future to have in our "toolbox". The second way was to place a hidden text box control on the web page and assign a validator to the hidden text box control. You next need code to populate the text box with the value of the checkbox. This way is very simple and has worked flawlessly.
The example below was copied from a blog that is reference below:
<%@ Control Language="C#" %>
<script runat="server">
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
_BoxMirror.Style.Add("display", "none"); // Hide the box. It is just there for the
// validation logic to work.
// Now hook up the check box to the text box. The text value mirrors the
// value of the check box in sync.
_Box.Attributes["onclick"] = "getElementById('" + _BoxMirror.ClientID + "').value=this.checked";
_BoxMirror.Text = _Box.Checked.ToString().ToLower(); // Sync text box and check box
// The validator Initial value is set to "false" so the Text Box will validate when
// its value is different from "false".
}
</script>
<div runat="server" id="_Wrapper" style="background-color:Yellow">
<asp:TextBox runat="server" ID="_BoxMirror" />
<asp:CheckBox runat="server" ID="_Box" />
<asp:RequiredFieldValidator InitialValue="false"
runat="server" Text="*" ID="_NeedToBeChecked" ControlToValidate="_BoxMirror" />
</div>
Special thanks to: http://cfouquet.blogspot.com/2006/07/validated-check-box-in-aspnet.html
