Data binding techniques for DropDownList controls

Source: Internet
Author: User
Tags bind empty

1. How to bind an enum to a DropDownList control

Dropdownlist1.datasource = Enum.getnames (typeof

(YSMV.            XWShop1B2C.Model.OrderStatus));

Dropdownlist1.databind ();

The main use of enum to bind an enum to the DropDownList control is GetNames (), which gets an array of enum names string[], and of course you can use GetValues () to get the value of the enum. This shows that the binding is actually binding DropDownList to an array.

2. How to bind an object list<t> to a DropDownList control

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 () method obtains a list<logisticinfo>, the key of the binding is to set the DropDownList DataTextField, Datavaluefield,name is Field of Logisticinfo.

3.DropDownList data binding method with Null first item

For example, to bind an object list<t> to a DropDownList control, the key is to set the value of the first item to NULL, so how do you set it?

We can simply set the first entry to be empty, as follows

Dropdownlist3.datasource = (new YSMV. XWShop1B2C.BLL.Logistics

().            GetAll ());

Dropdownlist3.datatextfield = "Name";

Dropdownlist3.datavaluefield = "Name";

Dropdownlist3.databind ();

Dropdownlist3.items[0].            Text = "";

Dropdownlist3.items[0]. Value = "";

To do so is to set the first item to empty, but the first item is not available, so this method is not advisable. And then I thought of the first entry. Insert a null phase, 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 ());

At the end, other binding methods I will continue to add, please pay attention.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.