If you have a drop down list control that you need to populate on page load and you want todo it through code, here is how:
#1: DropDownList1.DataSource = obj.PopulatVehicleNumber();
#2: DropDownList1.DataTextField = “Vehicleno”;
#3: DropDownList1.DataValueField = “Vehicleno”;
#4: DropDownList1.DataBind();
Explanation:
In #1 I have created an object of a class that has the data base connectivity implementation. What #1 does is to fetch you a table that you assign as a data source to the drop down list control.
#2 defines the text field for the drop down list. This is the values that the users will see and select from. The DataTextField is a property of the drop down list control. This property specifies which field from the DataSource is to be bound to the Text property of each item in the control.
#3 defines the value field for the text mentioned in #2. This will be the value that you will typically use to make some decisions on. For example, supposing a user selects a text “United States” from a Country drop down. You may decide to assign this text a id of say ‘0001’ as the value. You may then take logical decisions based on this field.
#4 calls the DataBind() method to bind the data to the drop down list control. The control remains empty until data binding occurs. This method used to bind data from a data source (eg. database) to a server control.
That’s it!!