I'm probably missing something really obvious (often the case). Already searched Google and didn't readily find an answer. I'm trying to find a way for a CompareValidator's ValueToCompare property to handle a DropDownList possibly having a null value when
bound to a bit field in a database.
Using asp.net 3.5, VS2008, and C# tied to an MS SQL backend. On an intake form, a clerical person inputs bare basic information. One of the columns the clerical person does
not input is tied to a nullable bit value in the database table. That leaves the column with a null value when the reviewer pulls up the more comprehensive form that includes a dropdownlist control tied to that column. Here is what the control and
the validator look like:
The first listitem in the dropdownlist handles the null value with no problem. However, I want to force the reviewer to select Yes or No before allowing the form to update. I've tried setting the validator to NotEqual and blank ("") and to NotEqual and "-1"
but neither worked. No errors but also the validator doesn't seem to trigger when the dropdownlist is still set to "Select Y/N." Any ideas how to set the validator to detect the null value in the dropdownlist? Any help appreciated.
Use a RangeValidator on the SelectedIndex of the DropDownList
Range needs to be greater than zero to pass validation, right?
Please click 'Mark as Answer' if reply assisted you.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
diverguy
Member
262 Points
427 Posts
CompareValidator comparing Null DropDowList value
Jan 14, 2010 05:06 PM|LINK
I'm probably missing something really obvious (often the case). Already searched Google and didn't readily find an answer. I'm trying to find a way for a CompareValidator's ValueToCompare property to handle a DropDownList possibly having a null value when bound to a bit field in a database.
Using asp.net 3.5, VS2008, and C# tied to an MS SQL backend. On an intake form, a clerical person inputs bare basic information. One of the columns the clerical person does not input is tied to a nullable bit value in the database table. That leaves the column with a null value when the reviewer pulls up the more comprehensive form that includes a dropdownlist control tied to that column. Here is what the control and the validator look like:
<asp:DropDownList ID="ddlDDCError" runat="server" SelectedValue='<%# Bind("DDCError") %>'> <asp:ListItem Value="">Select Y/N</asp:ListItem> <asp:ListItem Value="True">Yes</asp:ListItem> <asp:ListItem Value="False">No</asp:ListItem> </asp:DropDownList> <asp:CompareValidator ID="valDDCError" runat="server" ErrorMessage="*" ControlToValidate="ddlDDCError" Operator="NotEqual" ValueToCompare="-1" Text="*" />The first listitem in the dropdownlist handles the null value with no problem. However, I want to force the reviewer to select Yes or No before allowing the form to update. I've tried setting the validator to NotEqual and blank ("") and to NotEqual and "-1" but neither worked. No errors but also the validator doesn't seem to trigger when the dropdownlist is still set to "Select Y/N." Any ideas how to set the validator to detect the null value in the dropdownlist? Any help appreciated.
Thanks,
John
pixelsyndica...
Star
7826 Points
1344 Posts
Re: CompareValidator comparing Null DropDowList value
Jan 14, 2010 05:09 PM|LINK
Use a RangeValidator on the SelectedIndex of the DropDownList
Range needs to be greater than zero to pass validation, right?
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
tpeczek
Contributor
2112 Points
260 Posts
Re: CompareValidator comparing Null DropDowList value
Jan 14, 2010 06:16 PM|LINK
Probably the best way to go is RequiredFieldValidator:
<asp:RequiredFieldValidator ID="valDDCError" runat="server" ControlToValidate="ddlDDCError" InitialValue="" ErrorMessage="*"> </asp:RequiredFieldValidator>You should set the 'InitialValue' property to the value of default option in your DropDownList (default is String.Empty). Give it a try.
Yet another developer blog <-- visit my blog
diverguy
Member
262 Points
427 Posts
Re: CompareValidator comparing Null DropDowList value
Jan 14, 2010 06:53 PM|LINK
Great idea! Hadn't tried that one before. That solved the problem. Thanks.
John