Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with DropDownList and RequiredFieldValidator

Problem with DropDownList and RequiredFieldValidator

Scheduled Pinned Locked Moved ASP.NET
databasehtmlsql-servercomsysadmin
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    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 with Selected="True" Value="" The RequiredFieldValidator does not complain when ‘- select -’ is

    N P P 3 Replies Last reply
    0
    • K kbalias

      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 with Selected="True" Value="" The RequiredFieldValidator does not complain when ‘- select -’ is

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      You 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

      1 Reply Last reply
      0
      • K kbalias

        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 with Selected="True" Value="" The RequiredFieldValidator does not complain when ‘- select -’ is

        P Offline
        P Offline
        Parwej Ahamad
        wrote on last edited by
        #3

        As 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

        1 Reply Last reply
        0
        • K kbalias

          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 with Selected="True" Value="" The RequiredFieldValidator does not complain when ‘- select -’ is

          P Offline
          P Offline
          Prasanta_Prince
          wrote on last edited by
          #4

          In required field validator set InitialValue="0" ;
          Thank you.

          K 1 Reply Last reply
          0
          • P Prasanta_Prince

            In required field validator set InitialValue="0" ;
            Thank you.

            K Offline
            K Offline
            kbalias
            wrote on last edited by
            #5

            Thank you prasanta_prince In my RequiredFieldValidator I have set the InitialValue="" and it works great. Regards. Kobus

            P 1 Reply Last reply
            0
            • K kbalias

              Thank you prasanta_prince In my RequiredFieldValidator I have set the InitialValue="" and it works great. Regards. Kobus

              P Offline
              P Offline
              Prasanta_Prince
              wrote on last edited by
              #6

              @kbalias, Good to know your problem is solved.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups