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.