Search code examples
c#asp.netweb-user-controls

ASP.NET: How do I create radio buttons and databind them in a DetailsView?


I have a TemplateField in a DetailsView and its input should be one of a few choices in a lookup table. Currently it's a text field, but I want it to be a group of radio buttons, and it should work in both insert and edit mode (the correct current value should be selected in edit mode).

How do I create mutually exclusive radio buttons and databind them in a DetailsView TemplateField?

I'm on ASP.NET 3.5 using an Oracle database as a datasource.


Solution

  • <EditItemTemplate>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
            DataSourceID="LookupSqlDataSource" DataTextField="LOOKUPITEM_DESCRIPTION" 
            DataValueField="LOOKUPITEM_ID" SelectedValue='<%# Bind("ITEM_ID")%>'>
        </asp:RadioButtonList>
    </EditItemTemplate>
    
    <InsertItemTemplate>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
            DataSourceID="LookupSqlDataSource" DataTextField="LOOKUPITEM_DESCRIPTION" 
            DataValueField="LOOKUPITEM_ID" SelectedValue='<%# Bind("ITEM_ID")%>'>
        </asp:RadioButtonList>
    </InsertItemTemplate>