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

  • Question

  • User-2026951251 posted

    I know i'm getting this error because my database field is empty. How do I make the default selection to one of the items listed in the radiobuttonlist(which is inside a formview) when there is null value in the database or there is a value that doesn't match the items in the radiobuttonlist's listitems? Please help!

    Saturday, April 5, 2008 7:47 PM

Answers

  • User-2026951251 posted

    Actually I found out the solution. It was given in a blog.  

     Add an empty item to the list and then do some code in the OnDataBound event.  The code below works perfectly.

    Thanks to whoever the blog owns to 

    <asp:RadioButtonList ID="MyRadioButtonList" runat="server"
       SelectedValue='<%# Bind("Blah") %>'
       OnDataBound="MyRadioButtonList_DataBound">
          <asp:ListItem Value=""></asp:ListItem>
          <asp:ListItem Value="A"></asp:ListItem>
          <asp:ListItem Value="B"></asp:ListItem>
          <asp:ListItem Value="C"></asp:ListItem>
    </asp:RadioButtonList>

    protected void MyRadioButtonList_DataBound(object sender, EventArgs e)
    {
          RadioButtonList list = (RadioButtonList)sender;
          ListItem blank = list.Items.FindByValue("");
          if (blank != null)
             list.Items.Remove(blank);
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 5, 2008 10:04 PM

All replies

  • User-2026951251 posted

    Actually I found out the solution. It was given in a blog.  

     Add an empty item to the list and then do some code in the OnDataBound event.  The code below works perfectly.

    Thanks to whoever the blog owns to 

    <asp:RadioButtonList ID="MyRadioButtonList" runat="server"
       SelectedValue='<%# Bind("Blah") %>'
       OnDataBound="MyRadioButtonList_DataBound">
          <asp:ListItem Value=""></asp:ListItem>
          <asp:ListItem Value="A"></asp:ListItem>
          <asp:ListItem Value="B"></asp:ListItem>
          <asp:ListItem Value="C"></asp:ListItem>
    </asp:RadioButtonList>

    protected void MyRadioButtonList_DataBound(object sender, EventArgs e)
    {
          RadioButtonList list = (RadioButtonList)sender;
          ListItem blank = list.Items.FindByValue("");
          if (blank != null)
             list.Items.Remove(blank);
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 5, 2008 10:04 PM
  • User1564875471 posted

    in formView rowDatabound event handler ,you need something like this :

    [ don't forget to replace "foreignKeyField" with the DataBase column name that the selected value is binded to  ]

        Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
            Dim ForeginKeyField As Object = DataBinder.Eval(FormView1.DataItem, "foreignKeyField")
            Dim Radiolist As RadioButtonList = FormView1.FindControl("RadiobuttonList1")
    
            If ForeginKeyField Is DBNull.Value OrElse String.IsNullOrEmpty(ForeginKeyField) OrElse Radiolist.Items.FindByValue(ForeginKeyField) Is Nothing Then
    
                ' set the selection to some defual value or index ,.. i  will choose to select the first item 
                Radiolist.SelectedIndex = 0
    
            Else
                ' the value is exists and correct
                Radiolist.SelectedValue = ForeginKeyField
            End If
        End Sub
     
    Saturday, April 5, 2008 10:43 PM