Dropdownlist中的第一项空白(First Item on Dropdownlist in blank)

编程入门 行业动态 更新时间:2024-10-26 23:26:27
Dropdownlist中的第一项空白(First Item on Dropdownlist in blank)

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

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

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));

After your DataBind call, add this code.

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

更多推荐

本文发布于:2023-07-20 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空白   第一项   Dropdownlist   Item   blank

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!