asp.net - 下拉列表中的第一项为空白

标签 asp.net sql-server ado.net

如何将 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(); 

最佳答案

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

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

关于asp.net - 下拉列表中的第一项为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1075390/

相关文章:

sql-server - 如何在 T-SQL 中将日期时间转换为字符串

sql-server - SQL Server Management Studio 2005 和源代码管理

MySQL, MSSQL - SymmetricDS

.net - 我应该在数据访问层中保留sqlconnection吗?

sql - SQL 日期时间的粒度

asp.net - 在 Azure 上创建 ASP.NET MVC RESTful API 的指南

javascript - 当我在菜单上移动时显示 div

c# - 解压时出现Gzip魔数(Magic Number)问题

asp.net - "No files matched the search pattern"在 tfs 构建中

c# - 通过 ado.net 从 C# 调用 SQL Server 存储过程超时