gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:03:44 26 4
gpt4 key购买 nike

如何将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com