C #: Null database in RadioButtonList

I would like to have a ham radio if you can choose null.

Something like that:

<asp:RadioButtonList ID="rblCD" runat="server" SelectedValue='<%# Bind("tblCD") %>'>
           <asp:ListItem Value="RW">RW</asp:ListItem>
           <asp:ListItem Value="R">R</asp:ListItem>
           <asp:ListItem Value="DBNull">None</asp:ListItem>
 </asp:RadioButtonList>

      

Thanks a lot, Vincent

+2


a source to share


3 answers


The values ​​in the radioobuttonlist are always strings. You would have to do something like this

<asp:ListItem Value="">None</asp:ListItem>

      



Then when you read data from the control, do something like

if (rblCD.SelectedValue == string.Empty)
{
    MyDataRow["Column"] = DBNull.Value;
}

      

+1


a source


yo should use:



<asp:RadioButtonList runat=server ID="rd" 
SelectedValue='<%# Eval("myField").GetType() == typeof(DBNull) ? null : Eval("myField") %>'>
           <asp:ListItem Text="yes" Value="1"></asp:ListItem>
           <asp:ListItem Text="no" Value="2"></asp:ListItem>
</asp:RadioButtonList> 

      

+1


a source


yes you can do in your example

if(rblCD.SelectedValue== "DBNull")
{
     DataRow["Column"] = DbNull.Value;
}

      

0


a source







All Articles