Header Scripts

Validation controls in ASP.NET make it very easy to check data in forms if any information has been filled or if it meets certain criteria. It saves a great deal of time in hand coding JavaScript validation. Unfortunately, validation controls don’t work with the CheckBox or CheckBoxList controls. Your form may need to determine if a checkbox has been checked, or if a choice was made among a group of related checkboxes.

I found many solutions from searching on the Internet, but most of them didn’t work very well, if at all, and very few were designed for use with an asp:CheckBox. Many of the solutions used JavaScript or the JQuery framework. If I was using standard HTML controls (i.e. PHP development) I would use JavaScript to validate the controls.
After much trial and mostly error, I decided to find my own solution for validating if at least one checkbox was selected in a group of controls. I used C# code to determine if any one control was checked, followed by code to redirect the page to an anchor (i.e. <a name=”checkbox_controls”>Required</a>) on the current form. This code is placed in the form submission method (i.e. submitButton_Click) and placed before page validation (Page.IsValid), as in the following example
bool bIsValidChecked = CheckBox_Pickup_Option1.Checked || CheckBox_Pickup_Option2.Checked;
if (!bIsValidChecked )
Response.Write (“<script type=\”text/ecmascript\”>window.location.href = ‘#checkbox_controls’;</script>”);

// Set order time and session variables from the order form.
if (Page.IsValid & bIsValidChecked )
{

// form processing code
}
This isn’t the best solution, but its one that works quickly. I need to develop a custom control for validating checkboxes.

Print Friendly, PDF & Email
Translate »