【VB】加入一個空白Item在DropDownList最上方
觀念一:在SQL語法內加入一筆空白Item
SqlStr="select 0 as 縣市代號,'請選擇' as 縣市名稱 from dbo.縣市 " & _
"UNION " & _
"select 縣市代號,縣市名稱 from dbo.縣市 " & _
"order by 縣市代號, 縣市名稱 desc"
說明:第一句"select" 代表新增空白列
利用 UNION 方式將兩個Table合在一起
"order by"視情況而定不一定要加;為了要讓空白列放在第一筆
觀念二:在控制項內加入一筆空白Item
If IsPostBack = False Then
With DropDownList1
.DataSource = SqlDataSource1 '資料來源可以 DataTable, DataView 格式
.DataTextField = "縣市名稱"
.DataValueField = "縣市代號"
.DataBind()
End With
DropDownList1.Items.Insert(0, New ListItem("請選擇", String.Empty)) '在第一筆中插入
DropDownList1.SelectedIndex = 0 '預設 選擇第一筆
End If
使用前 ![]() | 使用後 ![]() |


留言
張貼留言