locked
'XXX' has a SelectedValue which is invalid because it does not exist in the list of items RRS feed

  • Question

  • User1644748805 posted

    'XXX' is the id of a radiobuttonlist control.

    This happens when I want to bind a sql data source to a radiobuttonlist control in formview.

     <asp:RadioButtonList ID="Gender" runat="server"  RepeatLayout="Flow" SelectedValue='<%# eval("Sex") %>'
                                    RepeatDirection="Horizontal" >
                                       
                                        <asp:ListItem Value="M">Male&nbsp;&nbsp;</asp:ListItem>
                                        <asp:ListItem Value="F">Female </asp:ListItem>
                                    </asp:RadioButtonList>

    I make a test record that the value is null in database. It seemed to me that the control can not be used when there were other values, such as '' ,null existed in the record, which does not make sense to me. Anyone knows what's going on, or have any suggestions? Thanks,

    Monday, October 22, 2007 11:48 PM

Answers

  • User-1476564377 posted

     Any value in the "Sex" field that is not M or F (the two listitem values) will cause this exception - you cannot avoid this as it is an intentional exception.. you cannot assign a value that does not exist in the list of items.

    You need to either ensure that only M or F is coming in from the datasource in the dataset, or alternatively wrap the SelectedValue assignment in a method
    e.g.    SelectedValue='<%# GetSex( eval("Sex") ) %>'

    ...where the getsex method has your rules for evaluating whether male or female
    e.g.
    public string GetSex(string sex)
    {
      if(sex!=null)
      {
          if(sex="M")
              return "M";
      }

     return "F";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2007 5:31 AM

All replies

  • User-1476564377 posted

     Any value in the "Sex" field that is not M or F (the two listitem values) will cause this exception - you cannot avoid this as it is an intentional exception.. you cannot assign a value that does not exist in the list of items.

    You need to either ensure that only M or F is coming in from the datasource in the dataset, or alternatively wrap the SelectedValue assignment in a method
    e.g.    SelectedValue='<%# GetSex( eval("Sex") ) %>'

    ...where the getsex method has your rules for evaluating whether male or female
    e.g.
    public string GetSex(string sex)
    {
      if(sex!=null)
      {
          if(sex="M")
              return "M";
      }

     return "F";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2007 5:31 AM
  • User1644748805 posted

    Thanks, foreachbiscuit 

    I know it is an exception, but I mean it is unrealistic. Users might leave that selection blank and they could do so. And we shouldn't just "default" that value to F or M, there should be an uncertain option in a real life.

     

    Tuesday, October 23, 2007 8:38 AM