Skip to main content

DropDownList in Asp.Net

Asp.Net DropDownList

                        Using DropDownList user can choose item from the list of item. To DropDownList include the tag <asp:DropDownList> in Asp page.
<asp:DropDownList ID="dd_year" runat ="server" >
                 <asp:ListItem  Value ="0">Select</asp:ListItem>
                 <asp:ListItem  Value ="1">I</asp:ListItem>
                 <asp:ListItem Value="2">II</asp:ListItem>
                 <asp:ListItem Value="3" >III</asp:ListItem>
The subtag <asp:ListItem > will add option, the attribute Value add integer value, for our convenience to use in code behind page.  TThe above dropdownlist static.

Default Option in DropDownList Asp.Net

<asp:RequiredFieldValidator id="rr_year" runat ="server" ForeColor ="red" ErrorMessage ="Select Year" ControlToValidate ="dd_year" InitialValue ="0"></asp:RequiredFieldValidator>
through RequireFieldValidator the default is set InitialValue="0".

Dynamic DropDownList Asp.Net

                            To Add ListItem in DropDropList dynamically use SqlDataSource. SqlDataSource will retrieve data from relational database and bind item to dropdownlist.
<asp:DropDownList  ID="dd_medium" Enabled ="false"  Visible ="true"   runat ="server"  Width ="250px" DataSourceID="SqlDataSourcemedium" DataTextField="medium" DataValueField="id" ></asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourcemedium" runat="server" ConnectionString ="<%$ ConnectionStrings:DefaultConnection%>" SelectCommand ="SELECT * from tbmedium"></asp:SqlDataSource>
Dropdownlist DataSourseID is set with SqlDataSource id.