简体   繁体   中英

How to add dropdown list default value

I am using asp.net for fetching data from sql table to dropdown list. The problem is that, when I give default selection to the dropdown list. It does not take the default value. Please see the code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("select * from States_agri", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        ddl_state.DataSource = dr;
        ddl_state.Items.Clear();
        ddl_state.Items.Add("--Please Select state--");
        ddl_state.DataTextField = "StateName";
        ddl_state.DataValueField = "StateID";
        ddl_state.DataBind();
        conn.Close();
    }
}
`

Also Please see the dropdown list aspx code for your reference.

<asp:UpdatePanel ID="FormUpdate" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddl_state" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <table>
            <tr>
                 <td>State*</td>
                 <td>
                     <asp:DropDownList ID="ddl_state" runat="server" CssClass="cbfld-popup1" AutoPostBack="true" OnSelectedIndexChanged="ddl_state_SelectedIndexChanged">
                     <asp:ListItem Enabled="true" Selected="True" Text="Please select State"></asp:ListItem>
                     </asp:DropDownList>
                 </td>

            </tr>
            <tr>
                 <td>District*</td>
                 <td>
                     <asp:DropDownList ID="ddl_district" CssClass="cbfld-popup1" runat="server">
                     <asp:ListItem Enabled="true" Selected="True" Text="Please select city"></asp:ListItem>
                                                                                                      </td>
            </tr>
         </table>
     </ContentTemplate>
</asp:UpdatePanel>

After data-binding, do this:

ddl_state.Items.Insert(0, new ListItem("Select","NA")

Or add it in markup as:

<asp:DropDownList .. AppendDataBoundItems="true">
   <Items>
       <asp:ListItem Text="Select" Value="" />
   </Items>
</asp:DropDownList>

你必须在Databind之后使用下面的menioned代码

ddl_state.Items.Insert(0, new ListItem("--Please Select state--", "0"));

You could set the AppendDataBoundItems to true in the declaration of your DropDownList

< asp:DropDownList AppendDataBoundItems="true" ...> ,

This is needed because as it is stated in MSDN

The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs.

Please have a look here .

you need to set AppendDataBoundItems="true" property in your dropdown list

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
 <asp:ListItem Text="Add New" Value="0" />
</asp:DropDownList>
ddl_state.Items.Insert(0, new ListItem("Select","NA")

if you want to fetch data from database den this will be helpful:

<asp:DropDownList id="MainCat_DropDownList" runat="server" Width="185px" AutoPostBack="True" DataSourceID="Maincategory_SqlDataSource" DataTextField="main_catname" DataValueField="main_catid" AppendDataBoundItems="True"></asp:DropDownList> <asp:SqlDataSource id="Maincategory_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT main_catid, Title_id, main_catname FROM main_cat WHERE (Title_id = 1)"></asp:SqlDataSource> </TD></TR><TR><TD align=right>Package</TD><TD align=left><asp:DropDownList id="MainPack_DropDownLis" runat="server" Width="185px" AutoPostBack="True" DataSourceID="MainPackage_SqlDataSource" DataTextField="pack_name" DataValueField="pack_id">
</asp:DropDownList>
<asp:SqlDataSource id="MainPackage_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>" SelectCommand="select main_catid,pack_id,pack_name from main_pack where main_catid=@main_catid">
   <SelectParameters>
     <asp:ControlParameter ControlID="MainCat_DropDownList" Name="main_catid" PropertyName="SelectedValue" />
   </SelectParameters>
</asp:SqlDataSource>

The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs.

if we will do this so it will append item from databases in it will add all items from column whenever we do selection and overloaded with all values from column.

Consider following example of : 1. DDL Country [if we will select DDLcountry so it should reflect DDLState] 2. DDL State [if we will select DDLstate so it should reflect DDLcity] 3. DDL City

but whenever we select new DDLcountry so DDLState should refelect with '--select state--' and DDLcity should be '--select City--'

在此示例中,第3个参数是默认值:

@Html.DropDownList("Items", new SelectList(ViewBag.Items), (string)ViewBag.MyFilter.ToString())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM