Problem with DropDownList and RequiredFieldValidator
-
Hi In a Sql Server database we have a Patient table and a ReferHospital table. The ReferHospital table is a lookup table. There is a Foreign key column (RefHospID) in the Patient table. In the past the Referring hospital was not a required input field, but it has become a required field for input recently. I have a DetailsView on the page and I have made one of the fields a TemplateField. In the EditItemTemplate and InsertItemTemplate I have a DropdownList that is databound to the ReferringHospital table in the database. So the two things I would like to achieve is: 1.) When an old patient record is loaded and the RefHospID field is null, the DropDownList must accept the null and display the initial value “- select -”. 2.) When a new patient is inserted (or an old patient edited) there must be a validation to check that the initial value “- select -” is not still selected, but that a RefHosp has been selected in the DropDownList. I manage to get these two things working individually but not combined. I set the AppendDataBoundItems property to true and added the initial value as a ListItem. Markup of the DropdownList
<asp:DropDownList ID="DropDownList_HP" runat="server"
AppendDataBoundItems="True" DataSourceID="SqlDataSource_RefHosp"
DataTextField="HospName" DataValueField="HospID" SelectedValue='<%# Bind("HospID") %>' Enabled="True">
<asp:ListItem Selected="True" Value="">- select -</asp:ListItem>
</asp:DropDownList>The code in bold is to enable the DropDownList to accept null values from the database. (This info I found at http://msdn.microsoft.com/en-us/library/ms366709.aspx ) I also added a RequiredFieldValidator to ensure that a value other than “- select -” is selected.
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
ControlToValidate="DropDownList_HP"
ErrorMessage="Select a referring hospital"
InitialValue="- select -" ValidationGroup="PatientInputGroupDV4"
SetFocusOnError="True"></asp:RequiredFieldValidator>The problem is that without
Selected="True" Value=""
In the ListItem of the DropDownList an exception is thrown which says the DropDownList has “SelectedValue which is invalid because it does not exist in the list of items.” And withSelected="True" Value=""
The RequiredFieldValidator does not complain when ‘- select -’ is -
Hi In a Sql Server database we have a Patient table and a ReferHospital table. The ReferHospital table is a lookup table. There is a Foreign key column (RefHospID) in the Patient table. In the past the Referring hospital was not a required input field, but it has become a required field for input recently. I have a DetailsView on the page and I have made one of the fields a TemplateField. In the EditItemTemplate and InsertItemTemplate I have a DropdownList that is databound to the ReferringHospital table in the database. So the two things I would like to achieve is: 1.) When an old patient record is loaded and the RefHospID field is null, the DropDownList must accept the null and display the initial value “- select -”. 2.) When a new patient is inserted (or an old patient edited) there must be a validation to check that the initial value “- select -” is not still selected, but that a RefHosp has been selected in the DropDownList. I manage to get these two things working individually but not combined. I set the AppendDataBoundItems property to true and added the initial value as a ListItem. Markup of the DropdownList
<asp:DropDownList ID="DropDownList_HP" runat="server"
AppendDataBoundItems="True" DataSourceID="SqlDataSource_RefHosp"
DataTextField="HospName" DataValueField="HospID" SelectedValue='<%# Bind("HospID") %>' Enabled="True">
<asp:ListItem Selected="True" Value="">- select -</asp:ListItem>
</asp:DropDownList>The code in bold is to enable the DropDownList to accept null values from the database. (This info I found at http://msdn.microsoft.com/en-us/library/ms366709.aspx ) I also added a RequiredFieldValidator to ensure that a value other than “- select -” is selected.
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
ControlToValidate="DropDownList_HP"
ErrorMessage="Select a referring hospital"
InitialValue="- select -" ValidationGroup="PatientInputGroupDV4"
SetFocusOnError="True"></asp:RequiredFieldValidator>The problem is that without
Selected="True" Value=""
In the ListItem of the DropDownList an exception is thrown which says the DropDownList has “SelectedValue which is invalid because it does not exist in the list of items.” And withSelected="True" Value=""
The RequiredFieldValidator does not complain when ‘- select -’ isYou could use a RangeValidator rather than a RequireFieldValidator. Assuming the selected value for the default item (-select-) is -1, set the MinimumValue attribute to 0 and the MaximumValue attribute to some number beyond the expected number of items, say 9999
I know the language. I've read a book. - _Madmatt
-
Hi In a Sql Server database we have a Patient table and a ReferHospital table. The ReferHospital table is a lookup table. There is a Foreign key column (RefHospID) in the Patient table. In the past the Referring hospital was not a required input field, but it has become a required field for input recently. I have a DetailsView on the page and I have made one of the fields a TemplateField. In the EditItemTemplate and InsertItemTemplate I have a DropdownList that is databound to the ReferringHospital table in the database. So the two things I would like to achieve is: 1.) When an old patient record is loaded and the RefHospID field is null, the DropDownList must accept the null and display the initial value “- select -”. 2.) When a new patient is inserted (or an old patient edited) there must be a validation to check that the initial value “- select -” is not still selected, but that a RefHosp has been selected in the DropDownList. I manage to get these two things working individually but not combined. I set the AppendDataBoundItems property to true and added the initial value as a ListItem. Markup of the DropdownList
<asp:DropDownList ID="DropDownList_HP" runat="server"
AppendDataBoundItems="True" DataSourceID="SqlDataSource_RefHosp"
DataTextField="HospName" DataValueField="HospID" SelectedValue='<%# Bind("HospID") %>' Enabled="True">
<asp:ListItem Selected="True" Value="">- select -</asp:ListItem>
</asp:DropDownList>The code in bold is to enable the DropDownList to accept null values from the database. (This info I found at http://msdn.microsoft.com/en-us/library/ms366709.aspx ) I also added a RequiredFieldValidator to ensure that a value other than “- select -” is selected.
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
ControlToValidate="DropDownList_HP"
ErrorMessage="Select a referring hospital"
InitialValue="- select -" ValidationGroup="PatientInputGroupDV4"
SetFocusOnError="True"></asp:RequiredFieldValidator>The problem is that without
Selected="True" Value=""
In the ListItem of the DropDownList an exception is thrown which says the DropDownList has “SelectedValue which is invalid because it does not exist in the list of items.” And withSelected="True" Value=""
The RequiredFieldValidator does not complain when ‘- select -’ isAs per my understanding and according to your dropdownlist value, initial value should be blank of Required field validator.
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
ControlToValidate="DropDownList_HP"
ErrorMessage="Select a referring hospital"
InitialValue="" ValidationGroup="PatientInputGroupDV4"
SetFocusOnError="True"></asp:RequiredFieldValidator>Parwej Ahamad ahamad.parwej@gmail.com
-
Hi In a Sql Server database we have a Patient table and a ReferHospital table. The ReferHospital table is a lookup table. There is a Foreign key column (RefHospID) in the Patient table. In the past the Referring hospital was not a required input field, but it has become a required field for input recently. I have a DetailsView on the page and I have made one of the fields a TemplateField. In the EditItemTemplate and InsertItemTemplate I have a DropdownList that is databound to the ReferringHospital table in the database. So the two things I would like to achieve is: 1.) When an old patient record is loaded and the RefHospID field is null, the DropDownList must accept the null and display the initial value “- select -”. 2.) When a new patient is inserted (or an old patient edited) there must be a validation to check that the initial value “- select -” is not still selected, but that a RefHosp has been selected in the DropDownList. I manage to get these two things working individually but not combined. I set the AppendDataBoundItems property to true and added the initial value as a ListItem. Markup of the DropdownList
<asp:DropDownList ID="DropDownList_HP" runat="server"
AppendDataBoundItems="True" DataSourceID="SqlDataSource_RefHosp"
DataTextField="HospName" DataValueField="HospID" SelectedValue='<%# Bind("HospID") %>' Enabled="True">
<asp:ListItem Selected="True" Value="">- select -</asp:ListItem>
</asp:DropDownList>The code in bold is to enable the DropDownList to accept null values from the database. (This info I found at http://msdn.microsoft.com/en-us/library/ms366709.aspx ) I also added a RequiredFieldValidator to ensure that a value other than “- select -” is selected.
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
ControlToValidate="DropDownList_HP"
ErrorMessage="Select a referring hospital"
InitialValue="- select -" ValidationGroup="PatientInputGroupDV4"
SetFocusOnError="True"></asp:RequiredFieldValidator>The problem is that without
Selected="True" Value=""
In the ListItem of the DropDownList an exception is thrown which says the DropDownList has “SelectedValue which is invalid because it does not exist in the list of items.” And withSelected="True" Value=""
The RequiredFieldValidator does not complain when ‘- select -’ isIn required field validator set InitialValue="0" ;
Thank you. -
In required field validator set InitialValue="0" ;
Thank you. -
Thank you prasanta_prince In my RequiredFieldValidator I have set the InitialValue="" and it works great. Regards. Kobus
@kbalias, Good to know your problem is solved.