WebForm控制項之DropDownList

來源:互聯網
上載者:User

標籤:

DropDwonList

三件事:

------------------------------------------1、把內容填進去-----------------------------------------

方法一:迴圈添加

DropDownList中的每一項都是ListItem類

例:List<Nation> list=查詢結果傳回值;

foreach(Nation data in list)

{

  DropDownList.Items.Clear();//每次用集合之前,先清空一下

  ListItem li=new ListItem(data.Name,data.Code)//重要的兩個參數1、text值(將來顯示給使用者)2、value值(用來傳遞資料)

  DropDownList1.Items.Add(li);

}

方法二:綁定添加

例:資料庫查詢返回list集合

DropDownList1.DataSource=list;

DropDownList1.DataTextField=“屬性名稱”;//text值(將來顯示給使用者)

DropDownList1.DataValueField=“屬性名稱”;//value值(用來傳遞資料)

DropDownList1.DataBind();//最後一定記得綁定資料

註:Items.Add()--此方法添加到集合

Items.Insert()--此方法可指定索引位置添加

------------------------------------------2、把選中的值取出來-----------------------------------------

使用屬性:

一、用於單選

1、SelectedValue

2、SelectedItem.Text 或者 SelectedItem.Value

3、SelectedIndex

二、用於多選情況:

1、foreach 遍曆DropDownList1.Items的集合

2、判斷每一項的li.Selected==true;

3、把li.Text或li.Value 取出來

------------------------------------------3、設定某項為選定項-----------------------------------------

DropDownList1.SelectedValue=值;

WebForm控制項之DropDownList

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.