Wednesday, 20 July 2011

Validate a CheckBox with CustomValidator in ASP4 and C#


This tutorial will demonstrate how to use a CustomValidator to add validation to a CheckBox with ASP.NET 4.0 and C#.

By default there is no validation control in ASP.NET to work with the checkbox control. However, in certain cases you may want to use validation on a checkbox such as a one that would agree to the terms and services of a web site.

We used over 10 web hosting companies before we found Server Intellect. Our new cloud server,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced

Adding the Default.aspx Page
To demonstrate this we will need to create a simple web page with a checkbox and a button on it. To do this:
  1. Right click the project in your solution explorer.
  2. Select add new item...
  3. Select a web form.
  4. Name it 'Default.aspx'.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a checkbox onto the web form.
  8. Drag and drop a customvalidator next to the checkbox.
    1. Change the ErrorMessage property to 'You must check the box'.
    2. Change the ForeColor property to 'Red'.
    3. Check the Text property to '*'.
  9. Add a line break after the customvalidator.
  10. Drag and drop a button under the checkbox.
  11. Add a line break after the button.
  12. Drag and drop a validationsummary under the button.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Validating the Checkbox
Next, we want to add in some simple code on the ServerValidate event of the customvalidator that we added. To do this:

  1. Double click the customvalidator to generate the ServerValidate event method.
  2. Add in the following code to the CustomValidator_ServerValidate event method:
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        //determine if CheckBox1 is checked or not, if it is validate it, else don't
        if (CheckBox1.Checked)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
Let's go over what this code is doing. Whenever validation is initiated, our ServerValidate event method is executed and checks to see if our checkbox is checked. If it is, then we change IsValid to true, if it is not we change IsValid to false.
 
Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we've found so far.

Testing
To test this out, simply load up the web site and click the button when the checkbox is not checked. You should receive the error message we added earlier. If you then click the button when the checkbox is checked, you should see no errors.

No comments:

Post a Comment