Friday, March 16, 2012

please help me with validation

Hi, I am trying to validate 3 HtmlInputCheckBox controls:
<input type="checkbox" id="cbx1" runat="server">
<input type="checkbox" id="cbx2" runat="server">
<input type="checkbox" id="cbx3" runat="server">
<asp:button id="validate" Text="OK" runat="server"/>
<asp:ValidationSummary id="valsummary" ShowSummary="False" ShowMessageBox="True" runat="server"/>
I need at least one of them to be checked and popup a messagebox (as of the validationsummary control) that tells the user if no checkbox is checked.
I appreciate your help!
Sincerely,
PeleP
There is no validator supplied with ASP.NET that can evaluate an individual checkbox. You have several choices:
1. Use a CustomValidator. Create your own client-side code to handle the evaluation. After all, to show the messagebox, you *must* have client-side validation. (Its just a requirement of the validator controls.)
2. Replace your checkboxes with a CheckBoxList control. The RequiredFieldValidator can detect that its blank and report an error. This is the easiest solution, but doesn't work if you have a format that doesn't match what the CheckBoxList allows.
3. Switch to an alternative validation system which can handle checkboxes. MyProfessional Validation And More supplies 25 validators including several involved for creating this case. You would use my MultiConditionValidator like this:
<vam:MultiConditionValidator id="MCV1" runat="server" ErrorMessage="Please select one" Operator="OR">
<Conditions>
<vam:CheckStateCondition ControlIDToEvaluate="cbx1" Checked="True" />
<vam:CheckStateCondition ControlIDToEvaluate="cbx2" Checked="True" />
<vam:CheckStateCondition ControlIDToEvaluate="cbx3" Checked="True" />
</Conditions>
</vam:MultiConditionValidator>
This will generate client-side support. It supports many more browsers than ASP.NET 1.x validation does. And its message box option shown when the user submits will show the error message.
Hi! Thanks for your reply!

I do not think the RequiredFieldValidator control works with checkboxlist controls. Maybe it does in "asp.net" 2.0?

Your validation controls look interesting but I think I will try to do it myself.
It seems to be a little complicated though, but I will give it a go.

Thanks anyway!

Sincerely,
PeleP
The RequiredFieldValidator does work with the CheckBoxList and RadioButtonList. It does not work with ordinary CheckBox and RadioButton controls.
strange... i have tried that, and i cannot make it work. can you give me a quick example how you would do it?
this is what i have learned:
<asp:checkboxlist id="cb1" runat="server">
...items...
</asp:checkboxlist>
<asp.requiredfieldvalidator id="rfv" controltovalidate="cb1" runat="server"/>
so, are you sure? this is the last time... i will try to not disturb you further with stupid questions...
Sincerely,
PeleP
It is my understanding that's how to set it up. The RequiredFieldValidator has a property called InitialValue which has a string that is the text representing "blank". It defaults to "". When evaluating any type of list control, if there is no selection, the control has a value of "" which should match to InitialValue="".
There is one more piece. Each <asp:ListItem> has a Value property too. These must be assigned to something other than "".
<asp:ListItem value="1">text here</asp:ListItem>

Sorry. The RequiredFieldValidator does NOT word with the CheckboxList.

Here's why -- Remember that the builtin validation controls also render client script. It gets the required validation property from the value attribute of the HTML element that gets rendered on the client. Since an HTML value attribute can't be two things at once, the validated control must have a single logical value on the client.

This is why RadioButtonList can be validated, but CheckBoxList cannot


Hi Lee,

You are correct. I don't know where my head was when I was writing those posts. I suspect I confused the RadioButtonList's support with the CheckBoxList. Oh well. Thanks for clearing it up.


Hello, thank you both for following up this post. Now I understand why it is not possible, so I can and will leave it for now. Maybe the new asp.net 2.0 can give a solution to this!? I will look into that later.

Anyway, I temporarily solved it by using ordinary CheckBox's and have validated them using some client code.

Thanks for your help!

Sincerely, PeleP


Yes, there are workarounds, thank goodness. Probably the easiest is to create a customvalidator with both server and client-side code.

ASP 2.0 does not provide a solution. Understand, the inability to validate the CheckBoxList never really was a limitation of ASP in the first place. It's really caused by the limitations of HTML. (Remember, no matter what, the server still has to render HTML at the end of the whole deal.) There is no value= attribute of HTML checkboxes that let them determine the state of other checkboxes. Therefore, a built-in ASP validator couldn't automatically render "generic" client-side code for it.

0 comments:

Post a Comment