Binding Data Source to DropDownList

Binding Data Source to Asp.Net Controls

Using the objects that Asp.Net offers us, we can connect a data source to list type controls such as dropdownlist, listbox. In other words, we can ensure that the records in our database are displayed in elements such as dropdownlist or listbox.

And we can do this without writing any code. If we know how to use the DataSource object (SqlDataSource etc.), our job is extremely easy.

First we need to add a DataSource object to our page and configure it. In other words, we must specify which database to connect to, which fields to choose from which table, etc.

Then we connect it to the relevant DataSource thanks to the DataSourceID property of the DropDownList object. In this way, the data coming with the DataSource is displayed in the list control.

For the process steps and pictorial explanations, see the example below:

When the City is Selected from DropDownList1, Listing the Districts of that City in DropDownList2

When a province is selected from one list, the updating and listing of districts in the other list is an example that we encounter frequently. For this example, we will first need a database containing the provinces and districts in Turkey.

Click to download the sample database created for Turkey.

The working state of our example will be as follows:

il seçildiğinde ilçeleri listeleme

When the province is selected from the DropDownList at the top, the districts belonging to that name appear in the lower one.

Process steps:

Since our database is Access, let's put a SqlDataSource on our page. (AccessDataSource in older versions) Let the name be provincesDataSource. Let's configure our data source with the Configure Data Source command and select both fields in the provinces table. (You can see it in the video.)

Now that our DataSource is ready, we can connect it to DropDownList:

il seçildiğinde ilçeleri listeleme

Let's click on the "Choose Data Source" command from the menu opened by clicking the arrow in the upper right corner of the DropDownList.

In the incoming window;

  • Let's choose which DataSource will be connected to this control from field 1.
  • The field to be used for the Text property of the DropDownList should be selected from the field number 2. In other words, from which field in the table the data that will appear in the list will come from, this must be specified. In our example, we select the city field so that the names of the cities appear in the list.
  • The field to be used as value should be selected from the field number 3. By selecting the Id field, we ensure that the city codes are used as the value of the DropDownList elements.

il seçildiğinde ilçeleri listeleme

When we click the OK button, the city names will now appear in the DropDownList.

Another point we should not forget is to enable the AutoPostBack feature of DropDownList1. Because when changes are made in this list, the page must be sent to the server and the list of districts must be renewed.

il seçildiğinde ilçeleri listeleme

Now it's time to prepare the DataSource that will fetch the districts (let it be named districtsDataSource). There are 3 fields in the counties table:

county: county name, id: county code and city: city code.

By entering the Where section in the DataSource configuration window, it should be ensured to obtain records whose city field is equal to the data from DropDownList1:

il seçildiğinde ilçeleri listeleme

In the figure below, let's indicate that the data in the city field will be compared with a control, and this control will be DropDownList1. Then click the Add button to add our condition to the query.

il seçildiğinde ilçeleri listeleme

Tamam deyip önceki ekrana döndüğümüzde Select sorgumuzun şu şekilde görünmesi gerekir.

il seçildiğinde ilçeleri listeleme

After we finish our DataSource configuration by saying Next, it remains to connect this DataSource to DropDownList2.

il seçildiğinde ilçeleri listeleme

And it's OK. Now, when the province is selected, the districts of that province will be listed in dropDownList2.

 

 

Connecting to the table in the Asp.net dropdownlist listbox database, displaying the records in the database in the dropdownlist listbox, the districts appearing at the bottom when you select a sample city, illustrated and video explanation

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 893 times.

Online Users: 11535



binding-data-source-to-dropdownlist