加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 下拉列表中的第一个项目为空白

发布时间:2020-12-16 07:37:05 所属栏目:asp.Net 来源:网络整理
导读:如何将DropDownList中的第一项放在空白处?在VB中就像DropDownList.index [0] =“”; 我这样做了: string StrConn = ConfigurationManager.ConnectionStrings["connSql"].ConnectionString; SqlConnection conn = new SqlConnection(StrConn); conn.Open();
如何将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));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读