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 a require input field DropDownList if old records have null in this field

Problem with a require input field DropDownList if old records have null in this field

Scheduled Pinned Locked Moved ASP.NET
databasehelphtmlsql-servercom
2 Posts 2 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 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 a table in my Sql Server database. I wanted to add an initial value to the DropdownList namely “- select -” . 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_HP"
    DataTextField="Hospital" DataValueField="H_ID" SelectedValue='<%# Bind("H_ID") %>' Enabled="True">
    asp:ListItem- select -</asp:ListItem>
    </asp:DropDownList>

    I also added a RequiredFieldValidator to ensure that a value is selected.

    <asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
    ControlToValidate="DropDownList_HP"
    ErrorMessage="Select a referring hospital"
    InitialValue="- select -" ValidationGroup="PatientInputGroupDV4" SetFocusOnError="True"></asp:RequiredFieldValidator>

    This works fine for new input. The problem is however that the database already has records that were entered through a Windows Application and many of these records has a null value in this field and exceptions are thrown when I tried to open these records and the system tried to set the SelectedValue of the DropdownList. After some more searching I found this help http://msdn.microsoft.com/en-us/library/ms366709.aspx So I changed the ListItem in the DropDownList markup to the following:

    <asp:DropDownList ID="DropDownList_RefHospDV4" runat="server"
    AppendDataBoundItems="True" DataSourceID="SqlDataSource_RefHosp"
    DataTextField="HospShort" DataValueField="HospID" SelectedValue='<%# Bind("HospID") %>' Enabled="True">
    <asp:ListItem Selected="True" Value="">- select -</asp:ListItem>
    </asp:DropDownList>

    This now solved the problem of opening a record where the value is null in the database, BUT now the RequiredFieldValidator is not validating anymore to make sure that a databound item is selected for this field and not the initial value “- select -”. So basically now it is not checking anymore to see if valid input has been entered for the DropDownList and it accep

    K 1 Reply Last reply
    0
    • K kbalias

      Hi 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 a table in my Sql Server database. I wanted to add an initial value to the DropdownList namely “- select -” . 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_HP"
      DataTextField="Hospital" DataValueField="H_ID" SelectedValue='<%# Bind("H_ID") %>' Enabled="True">
      asp:ListItem- select -</asp:ListItem>
      </asp:DropDownList>

      I also added a RequiredFieldValidator to ensure that a value is selected.

      <asp:RequiredFieldValidator ID="RequiredFieldValidatorDV4" runat="server"
      ControlToValidate="DropDownList_HP"
      ErrorMessage="Select a referring hospital"
      InitialValue="- select -" ValidationGroup="PatientInputGroupDV4" SetFocusOnError="True"></asp:RequiredFieldValidator>

      This works fine for new input. The problem is however that the database already has records that were entered through a Windows Application and many of these records has a null value in this field and exceptions are thrown when I tried to open these records and the system tried to set the SelectedValue of the DropdownList. After some more searching I found this help http://msdn.microsoft.com/en-us/library/ms366709.aspx So I changed the ListItem in the DropDownList markup to the following:

      <asp:DropDownList ID="DropDownList_RefHospDV4" runat="server"
      AppendDataBoundItems="True" DataSourceID="SqlDataSource_RefHosp"
      DataTextField="HospShort" DataValueField="HospID" SelectedValue='<%# Bind("HospID") %>' Enabled="True">
      <asp:ListItem Selected="True" Value="">- select -</asp:ListItem>
      </asp:DropDownList>

      This now solved the problem of opening a record where the value is null in the database, BUT now the RequiredFieldValidator is not validating anymore to make sure that a databound item is selected for this field and not the initial value “- select -”. So basically now it is not checking anymore to see if valid input has been entered for the DropDownList and it accep

      K Offline
      K Offline
      ktrrzn
      wrote on last edited by
      #2

      Hi Kobus, I suggest this solution: use javascript to check ur default value b4 post it to server.

      var sDropDownList_HP = '<%=DropDownList_HP.ClientID %>';
      // var slblMsg = '<%=lblMsg.ClientID %>';

      function validateHospitalDropDownList()
      {
      var ddlHP = document.getElementById(sDropDownList_HP);
      if(ddlHp.selectedIndex<1){
      // document.getElementById(slblMsg).innerHTML= "Please select Hospital";
      return false;
      }
      else{
      return true;
      }

      }

      In your asp code: there may be some button click event to post back to server: I assume u have that button,

      OnClientClick="if(!validateHospitalDropDownList())return false;" Text="Submit" />

      then add attribute OnClientClick and assign JS function to it, if this function return true, the form will post back normally, else no post back will occur. you can also add some code to show error msg in the JS function. Hint: in Data Null case, u can use ISNULL('value that can be null', 'new replace value'); function from SQL. Hope that works!

      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