基本控制項提示之–DropDownList資料繫結

來源:互聯網
上載者:User

1.將Enum綁定到DropDownList控制項的方法

Code
            DropDownList1.DataSource = Enum.GetNames(typeof(YSMV.XWShop1B2C.Model.OrderStatus));
            DropDownList1.DataBind();

       將Enum綁定到DropDownList控制項的主要用到Enum的是GetNames(),該方法得到的是一個Enum名稱的數組string[],當然你也可以使用GetValues()獲得Enum的數值。由此可見該綁定實際是將DropDownList綁定到一個數組。

2.將對象List<T>綁定到DropDownList控制項的方法

Code
1            DropDownList3.DataSource = (new YSMV.XWShop1B2C.BLL.Logistics().GetAll());
2             DropDownList3.DataTextField = "Name";
3             DropDownList3.DataValueField = "Name";
4             DropDownList3.DataBind();

        new YSMV.XWShop1B2C.BLL.Logistics().GetAll()方法獲得一個List<LogisticInfo>,綁定的關鍵在於設定DropDownList的DataTextField ,DataValueField,name便是 LogisticInfo的field.

3.DropDownList資料繫結第一項為空白的方法

以將對象List<T>綁定到DropDownList控制項的方法為例,關鍵在於設定第一項的值為空白,那如何設定呢?

我們可以直接設定第一項為空白,如下

 

Code
            DropDownList3.DataSource = (new YSMV.XWShop1B2C.BLL.Logistics().GetAll());
            DropDownList3.DataTextField = "Name";
            DropDownList3.DataValueField = "Name";
            DropDownList3.DataBind();
            DropDownList3.Items[0].Text = "";
            DropDownList3.Items[0].Value = "";

這麼做是將第一項設定為空白了,但是原來第一項的內容沒有了,那來此法不可取。於是想到了再第一項的位置插入一個空相,代碼:

Code
1             DropDownList3.DataSource = (new YSMV.XWShop1B2C.BLL.Logistics().GetAll());
2             DropDownList3.DataTextField = "Name";
3             DropDownList3.DataValueField = "Name";
4             DropDownList3.DataBind();
5             DropDownList3.Items.Insert(0, new ListItem());

 

末,其他Binder 方法我將繼續添加,請關注。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.