下拉列表中的第一个项目为空白

时间:2022-03-27 08:47:06

How to put the first item on the DropDownList in blank ? In VB is something like, DropDownList.index[0] = ""; I did this:

如何将DropDownList中的第一项放在空白处?在VB中就像DropDownList.index [0] =“”;我这样做了:

string StrConn = ConfigurationManager.ConnectionStrings["connSql"].ConnectionString;
    SqlConnection conn = new SqlConnection(StrConn);

    conn.Open();

    SqlDataReader dr;

    string sql;
    sql = @"Select Nome From DanielPessoas";

    SqlCommand cmd = new SqlCommand(sql, conn);
    dr = cmd.ExecuteReader();

    DropDownList1.DataSource = dr;
    DropDownList1.DataTextField = "Nome";
    DropDownList1.DataValueField = "Nome";
    DropDownList1.DataBind(); 

5 个解决方案

#1


After your DataBind call, add this code.

在DataBind调用之后,添加此代码。

DropDownList1.Items.Insert(0, new ListItem(string.Empty, string.Empty));

#2


You can define the empty item in aspx file if you set AppendDataBoundItems property to true.

如果将AppendDataBoundItems属性设置为true,则可以在aspx文件中定义空项。

<asp:DropDownList ID="ddlPersons" runat="server" AppendDataBoundItems="true" DataValueField="ID" DataTextField="Name">
    <asp:ListItem> -- please select person -- </asp:ListItem>
</asp:DropDownList>

Then you can databind items from the database in the codebehind:

然后,您可以在代码隐藏中对数据库中的项进行数据绑定:

ddlPersons.DataSource = personsList;
ddlPersons.DataBind();

I regard this "empty item" as presentation / UI, so I like to put it in the aspx. It also keeps the codebehind simpler.

我认为这个“空项目”是演示文稿/ UI,所以我喜欢把它放在aspx中。它还使代码隐藏更简单。

#3


Do something like this: Here is a simple example

做这样的事情:这是一个简单的例子

<asp:DropDownList ID="ddlFruits" runat="server">

        <asp:ListItem Value="1" Text="Oranges"></asp:ListItem>
        <asp:ListItem Value="2" Text="Apples"></asp:ListItem>
        <asp:ListItem Value="3" Text="Grapes"></asp:ListItem>
        <asp:ListItem Value="4" Text="Mangoes"></asp:ListItem>
    </asp:DropDownList>

And in the code behind

并在代码背后

ddlFruits.Items.Insert(0, new ListItem(string.Empty, "0"));

#4


You can do it with this way:

你可以这样做:

dropdownlist1.Items.Insert(0, new ListItem("Select here...", string.Empty));

#5


You can do it in SQL:

你可以在SQL中做到这一点:

sql = @"Select Nome From DanielPessoas UNION ALL Select '' Order by Nome";

#1


After your DataBind call, add this code.

在DataBind调用之后,添加此代码。

DropDownList1.Items.Insert(0, new ListItem(string.Empty, string.Empty));

#2


You can define the empty item in aspx file if you set AppendDataBoundItems property to true.

如果将AppendDataBoundItems属性设置为true,则可以在aspx文件中定义空项。

<asp:DropDownList ID="ddlPersons" runat="server" AppendDataBoundItems="true" DataValueField="ID" DataTextField="Name">
    <asp:ListItem> -- please select person -- </asp:ListItem>
</asp:DropDownList>

Then you can databind items from the database in the codebehind:

然后,您可以在代码隐藏中对数据库中的项进行数据绑定:

ddlPersons.DataSource = personsList;
ddlPersons.DataBind();

I regard this "empty item" as presentation / UI, so I like to put it in the aspx. It also keeps the codebehind simpler.

我认为这个“空项目”是演示文稿/ UI,所以我喜欢把它放在aspx中。它还使代码隐藏更简单。

#3


Do something like this: Here is a simple example

做这样的事情:这是一个简单的例子

<asp:DropDownList ID="ddlFruits" runat="server">

        <asp:ListItem Value="1" Text="Oranges"></asp:ListItem>
        <asp:ListItem Value="2" Text="Apples"></asp:ListItem>
        <asp:ListItem Value="3" Text="Grapes"></asp:ListItem>
        <asp:ListItem Value="4" Text="Mangoes"></asp:ListItem>
    </asp:DropDownList>

And in the code behind

并在代码背后

ddlFruits.Items.Insert(0, new ListItem(string.Empty, "0"));

#4


You can do it with this way:

你可以这样做:

dropdownlist1.Items.Insert(0, new ListItem("Select here...", string.Empty));

#5


You can do it in SQL:

你可以在SQL中做到这一点:

sql = @"Select Nome From DanielPessoas UNION ALL Select '' Order by Nome";