One ASP.NET form has a GridView on it that reads records from an Access database. In this case, it’s a football schedule for a particular team for this year. A button on each record displays another form with a DetailsView control for the purpose of displaying, editing or deleting that particular record. There are fields such as “Won/Lost” or “Home/Away” that are bound to textboxes, but the desire was to limit the options for the user to enter. A DropDownList would be the answer, and it would force the user to pick from the list rather than being able to enter anything. Below is the code that replaces a textbox with a DropDownList with limited options.

The textbox on the DetailsView control is whether the football game is either “Home” or “Away“.  These two options are presented in a DropDownList.  Original code in the <FIELDS> part of the page:

<asp:BoundField DataField=”HomeOrAway” HeaderText=”HomeOrAway” SortExpression=”HomeOrAway” />

New code that replaces the textbox with a DropDownList with specific list items:

 <asp:TemplateField HeaderText=”HomeOrAway”>
    <EditItemTemplate>
        <asp:DropDownList ID=”DropDownList1″
             runat=”server”
             DataTextField = “HomeOrAway”
             DataValueField = “HomeOrAway”
             SelectedValue='<%# Bind(“HomeOrAway”) %>’ >
             <asp:ListItem>Home</asp:ListItem>
             <asp:ListItem>Away</asp:ListItem>
       </asp:DropDownList>
   </EditItemTemplate>
   <ItemTemplate>
         <asp:Label Runat=”server”
             Text='<%# Bind(“HomeOrAway”) %>’ ID=”Label1″>
         </asp:Label>
  </ItemTemplate>
</asp:TemplateField>