如何添加下拉列表默认值

时间:2014-06-13 10:41:32

标签: c# asp.net

我使用asp.net从sql表获取数据到下拉列表。问题是,当我将默认选择提供给下拉列表时。它不采用默认值。 请参阅代码

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();
    }
}
`

另请参阅下拉列表aspx代码供您参考。

<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>

8 个答案:

答案 0 :(得分:16)

数据绑定后,请执行以下操作:

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

或者将其添加到标记中:

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

答案 1 :(得分:3)

你必须在Databind

之后使用以下的代号
ddl_state.Items.Insert(0, new ListItem("--Please Select state--", "0"));

答案 2 :(得分:1)

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

答案 3 :(得分:0)

您可以在AppendDataBoundItems

的声明中将DropDownList设置为true

&lt; asp:DropDownList AppendDataBoundItems="true" ...>

这是必需的,因为正如MSDN中所述

  

AppendDataBoundItems属性允许您添加项目   ListControl对象数据绑定发生之前。

请查看here

答案 4 :(得分:0)

您需要在下拉列表中设置AppendDataBoundItems="true"属性

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
 <asp:ListItem Text="Add New" Value="0" />
</asp:DropDownList>

答案 5 :(得分:0)

如果你想从数据库中获取数据,这将有所帮助:

<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>

答案 6 :(得分:0)

AppendDataBoundItems属性允许您在数据绑定发生之前将项添加到ListControl对象。

如果我们这样做,那么它将从数据库中附加项目,只要我们选择并从列中重载所有值,就会在列中添加所有项目。

考虑以下示例: 1. DDL国家[如果我们选择DDLcountry,那么它应该反映DDLState] 2. DDL状态[如果我们选择DDLstate所以它应该反映DDLcity] 3. DDL City

但是每当我们选择新的DDLcountry时,DDLState应该反复选择&#39; - 选择状态 - &#39;和DDLcity应该是 - 选择城市 - &#39;

答案 7 :(得分:0)

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

srv/users/JACK