locked
Problems with Null values and Radiobuttonlist RRS feed

  • Question

  • User1196392296 posted

    Hi guys,

     I've been trawling various forums and racking my brain over this one for the last couple of days. I have a problem with a databound radiobuttonlist inside a formview, as when a new record is created the value for the field bound to to radiobuttonlist is null which causes an error because the value is ot in the list of items. I could add an extra item to handle this, but I can't think of a way to make it invisible so it looks silly, and I could add a default value to the DB field, but I want to make sure the selection has actually been made by the user. The only way around it I can think of at the moment is to create a server control based on the original, or remove the databinding and do it manually.

    Any help would be most appreciated.

     As an aside, I've seen this type of question a number of times but haven't found a satisfactory answer and a couple of days ago I came upon an ASP site with radio buttons where the first item in the list was something like "Please select an option from below" and was pre-selected. I though that looked a little silly too.

    Thursday, February 1, 2007 3:23 AM

All replies

  • User1406895847 posted
    Pls post some of the code to check
    Thursday, February 1, 2007 10:05 AM
  • User1091955703 posted

    I am trying to understand the problem but it's not sinking in with me [*-)]

    I gather the problem is the NULL field, but I am not sure what you want to do with it.

    Can you just modify your query "WHERE DataBoundField IS NOT NULL" and that will leave out any records with NULL in that field.

    Or you can do SELECT ISNULL( DataBoundField, 'This Field Is Null' ) AS DataBoundField FROM...
    and this will replace NULL values with Text of your choice ( use '' for empty string ). This is probably along the lines of the Please Select method.

    If I have missed the mark please post an example of what happens and what you need to happen.

    Friday, February 2, 2007 12:04 AM
  • User1196392296 posted

    Sorry, I may not have explained it clearly.

    I have a new workaround now, which I created based on one I found elsewhere. It has the any empty item as the last item in my RadioButtonList and when the Fromview databound event fires it checks through the formview controls deleting any blank items from any RadioButtonLists, so I can have a few of them on each page. It seems to work in testing, so heres the code if anyone is interested.

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        LoopThroughControls(FormView1)
    End Sub

    Private Sub LoopThroughControls(ByVal MyParentControl As Control)
    Dim MyControl As Control

    For Each MyControl In MyParentControl.Controls
        If MyControl.HasControls Then LoopThroughControls(MyControl)
        If TypeOf MyControl Is RadioButtonList Then
            Dim MyList As RadioButtonList = CType(MyControl, RadioButtonList)
            Dim blank As ListItem = MyList.Items.FindByValue("")
            Try
                MyList.Items.Remove(blank)
            Catch ex As Exception
            End Try
        End If
        Next
    End Sub

    Friday, February 2, 2007 6:30 AM
  • User-2018420669 posted

    Hey Abbey Crunch, I've been having the same problem myself, getting the RadioButtonList to bind to a NULL value. I finally figured it out, and its actually quite simple:

    1. Create the RadioButtonList

    2. Go to the List Item Collection Editor of the RadioButtonList

    3. Add the TEXT that should be displayed if the bounded field is NULL. I used "N/A"

    4. In the VALUE box, put "NULL" (any value that IS NOT the same as the text will work)

    5. Switch to Source View, and locate the RadioButtonList in whatever form and template it is in

    6. You should see:

    <asp:ListItem Value="NULL">N/A</asp:ListItem>

    7. Remove the NULL, and just leave the "", with nothing inside.

    8. Save, and run your project. Now your RadioButtonList will select N/A for any record that has a null value.

     

    I'm pretty sure you can do this with a dropdownlist as well. Please let me know if this did not work for you.

     

     

    Thursday, February 15, 2007 3:07 PM
  • User1275668648 posted

    If you want the 'null' option to be invisible, you can hide it by adding a style tag to the Listitem, in your aspx page:

    <asp:ListItem Value="" style="display:none">null</asp:ListItem>

    caution, if you use style="visibility:hidden", the hidden list item will create a blank place-holder on the form, which you probably don't want, and the hidden item can still be selected by the arrow keys, which you probably don't want.
    Thursday, April 24, 2008 6:18 PM
  • User1218042982 posted

     Thank you as this is the only solution after days of digging I could find that works for my situation as well. Thanks so much!

    Monday, February 2, 2009 3:48 PM
  • User-412844084 posted

    Tanks it solved my problem too:) 

    Tuesday, December 14, 2010 4:32 PM
  • User873319012 posted

    If you want the 'null' option to be invisible, you can hide it by adding a style tag to the Listitem, in your aspx page:

    <asp:ListItem Value="" style="display:none">null</asp:ListItem>

    caution, if you use style="visibility:hidden", the hidden list item will create a blank place-holder on the form, which you probably don't want, and the hidden item can still be selected by the arrow keys, which you probably don't want.

    Nice one, hehehe.

    Sunday, June 19, 2011 2:34 PM
  • User808707720 posted

    RadioButtonList1.SelectedValue = Nothing

    Thursday, October 9, 2014 4:40 AM