I've been programming for over 5 years now and it was just within the last year that I found the need to validate an asp.net checkbox list control. I'm telling you, never had a need! Now I know why I never had the need, it was because it was a premonition of what a pain it was going to be until I found this little snippet of code below.
Actually, here's the deal. There is not a standard "Required Field Validator" control for checkbox lists standard in your toolbox. So what you do is you use a Custom. Validator and add an OnServerValidate attribute calling the procedure below. (however for my blogger purposes the < and > were replaced with [ and ])
[asp:CustomValidator ID="cvAlarmType" runat="server" CssClass="ErrorFont" OnServerValidate="AlarmTypeCBL_ServerValidate"][/asp:CustomValidator]
Then add the following code in your code behind.....
Protected Sub CustomValidation_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvAlarmServices.ServerValidate
Dim i As Integer
i = 0
For Each item As ListItem In Me.checkboxlist.Items
If item.Selected Then
i = i + 1
If i = 0 Then
args.IsValid = False
Me.CustomValidator1.ErrorMessage = "Selection Required"
Me.cvAlarmServices.ErrorMessage = ""
Now, one "gotcha" I've noticed is that if you are using other validation controls on your page and you have a validation summary that is popping up, well, the popup stops working for some reason. But hey, when I figure that one out, I'll be sharing the info!!
Yes I am one of those people who:
- Didn't pre-order my Droid X
- Wants a Droid X
- Will be heading to my local Verizon store at 8am to get my Droid X
- Will be totally frantic if I walk out of that store without a Droid X
It just so happens that I have some appointments on Thursday that I had to take a vacation day from work to do. When I realized that Thursday was the release date of the Motorola Droid X, I had to take advantage of it for one reason and one reason only.
My husband pre-ordered his Droid X from Best Buy and I want to get one before him.
Sure, I want the phone but why go so far as to plan ahead and pre-order when I can spend my morning on a treasure hunt like adventure, only the treasure isn't gold, it's the Droid X!
Well, okay so I've kind of cheated, I talked to a rep from my local Verizon Store and basically set an appointment with him to pick up my Droid X at 12:30, so it's not like I'm digging on exotic beaches or threatening Verizon sales reps for it.
But will that stop me from driving to the other Verizon stores after dropping my son off at 8am to see if I can get one before 12:30? Hell no!
Bring on my Droid X!
PS. Could I possibly link to Droid X anymore???
- I am not a domestic diva.
- I dislike dishes and mopping.
- Laundry is not edgy.