下拉列表添加默认名称

时间:2013-12-20 06:36:08

标签: c# asp.net drop-down-menu

我有一个包含所有国家/地区名称的下拉列表。代码工作正常,但我需要在下拉列表中添加默认选项。如何添加默认选项?(< - choose - >)。 我的代码是

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ddlCountryofOrgin.ascx.cs" Inherits="UserControls_DataType_ddlCountryofOrgin" %>
<asp:DropDownList    ID="ddlCountry" CssClass="ddlCountry" runat="server" DataValueField="english_name"
    DataSourceID="sqlCountry" OnPreRender="ddlCountry_PreRender" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" />

<asp:SqlDataSource  ID="sqlCountry" runat="server" SelectCommand="SELECT [country_id],[english_name], danish_name  FROM [dbo].[nano_country] WHERE [is_active] = 1 ORDER BY [sort_order] DESC, [english_name] ASC" />

提前感谢您的帮助......

3 个答案:

答案 0 :(得分:3)

AppendDataBoundItems设为True

并将项目添加到下拉列表:

<asp:ListItem Selected="True" Value="0">--Choose--</asp:ListItem>

代码:

<asp:DropDownList    ID="ddlCountry" CssClass="ddlCountry" runat="server" AppendDataBoundItems = "True" DataValueField="english_name"
    DataSourceID="sqlCountry" OnPreRender="ddlCountry_PreRender" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" >
     <asp:ListItem Selected="True" Value="0">--Choose--</asp:ListItem>
</asp:DropDownList > 

答案 1 :(得分:0)

protected void Page_Load(object sender, system.EventArgs as e)
{
ddlCountry.Items.Insert(0,"--Choose--");
}

答案 2 :(得分:0)

试试此代码

ddlCountry.Items.Insert(0, new ListItem("Select","--Choose--")