Hi all,
I have two dropdownlist (country and city dropdownlists) taking their data from access database, in order for filtering the data shown in gridview.

I want city dropdownlist get notified and change whenever a country selected from country dropdown list

I mean, wheneever user selects a country, I want city dropdownlist to show only cities only from the selected country

Right now I only use ASP NET (behind code),
In order to make that dynamic dropdownlist, do I hav to write c# code ?
can someone show how cna I do it with ASP NEt or c#?


And here is my full ASP NET code

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<title>Untitled Page</title>
    <link rel="stylesheet" type="text/css" href="gridview.css" media="all" />

    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <h3>Gridview with Filtering</h3>
            <div class="GridviewDiv">
            <table style="width: 540px" border="0" cellpadding="0" cellspacing="1" class="GridviewTable">
                <tr >

                    <td onmouseover="this.style.background='red';this.style.cursor='pointer'"
        onmouseout="this.style.background='orange';" onclick="City='http://www.google.com'">
        ID </td>                    

                    <td style="width: 120px;" >
                        First Name
                    <td style="width: 120px;">
                        Last Name
                    <td style="width: 130px;">

                    <td style="width: 130px;">


                <tr >
                    <td style="width: 40px;" >


                    <td style="width: 120px;">
                     <td style="width: 130px;">

                    <td style="width: 130px;">
                        <asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true" 
                            AutoPostBack="true" DataSourceID="dsPopulateCountry" 
                            DataValueField="Country" Font-Size="11px" Height="16px" Width="120px">
                            <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    <td style="width: 130px;">
                        <asp:DropDownList ID="ddlCity" runat="server" AppendDataBoundItems="true" 
                            AutoPostBack="true" DataSourceID="dsPopulateCity" DataValueField="City" 
                            Font-Size="11px" Width="120px">
                            <asp:ListItem Text="All" Value="%"></asp:ListItem>
                    <td colspan="5">

            <asp:hyperlinkfield headertext="Title"
            datanavigateurlfields="id" />
                                <asp:BoundField DataField="id" HeaderText="Sırala" SortExpression="id" ItemStyle-Width="40px"
                                    ItemStyle-HorizontalAlign="Center"  />
                                <asp:BoundField DataField="FirstName" HeaderText="Sırala" SortExpression="FirstName"
                                    ItemStyle-Width="120px" />
                                <asp:BoundField DataField="LastName" HeaderText="Sırala" SortExpression="LastName"
                                    ItemStyle-Width="120px" />
                                <asp:BoundField DataField="Country" HeaderText="Sırala" SortExpression="Country"
                                    ItemStyle-Width="130px" />
                                <asp:BoundField DataField="City" HeaderText="Sırala" SortExpression="City"
                                    ItemStyle-Width="130px" />

            <asp:AccessDataSource ID="dsGridview" runat="server" DataSourceMode="DataSet"

            SelectCommand="SELECT * FROM T_Employees"
FilterExpression="Country like '{0}%'  and City like '{1}%' ">
                    <asp:ControlParameter Name="Country" ControlID="ddlCountry" PropertyName="SelectedValue" />
                    <asp:ControlParameter Name="City" ControlID="ddlCity" PropertyName="SelectedValue" />


            <asp:AccessDataSource ID="dsPopulateCountry" runat="server" DataFile="Database2.mdb"  
                SelectCommand="SELECT DISTINCT Country from T_Employees"></asp:AccessDataSource>
            <asp:AccessDataSource ID="dsPopulateCity" runat="server" DataFile="Database2.mdb"  
                SelectCommand="SELECT DISTINCT City FROM T_Employees"></asp:AccessDataSource>
            <asp:AccessDataSource ID="dsPopulateFirstName" runat="server" DataFile="Database2.mdb"  
                SelectCommand="SELECT DISTINCT FirstName FROM T_Employees"></asp:AccessDataSource>
             <asp:AccessDataSource ID="dsPopulateLastName" runat="server" DataFile="Database2.mdb"  
                SelectCommand="SELECT DISTINCT LastName FROM T_Employees"></asp:AccessDataSource>

Have a selected event on the country dropdown that gets fired when someone changes what is selected. Now when the event fires, it looks to see what is selected and pulls the information from the database and fills the cities dropdown with the relevant data.

Do I have to make a query on the database for every time am event happens,
or can I use gridview for updating dropdownlist ?

Can you provide a litle code for this ?

I'm not familiar with a method of doing this without using code, however, take that with a grain of salt, as I'm not a fan of doing everything on the asp page, seperating with a code file makes for cleaner asp markup and code pages, in my humble opinion.

That said, using a code behind would be quite simple.

add a little to your Country Drop Down List:

<asp:DropDownList ID="ddlCountry" runat="server" 
AutoPostBack="true" OnSelectedIndexChanged="PopulateCitiesDropDown" Font-Size="11px" Height="16px" Width="120px">

(note the wrapping of code in code tags, makes it look nice and easy to read!)

Then in your code behind, add some simple code

proteced void PopulateCitiesDropDown(Object countryDropDown, EventArgs e)
//in here is where you populate the data.  you can do some really cool stuff with drop downs and bindings, but I wont go into more detail
string Country = ddlCountry.Text;
//use the country value to find what you need

The gist inside the code behind is to create a set of data to populate the drop down.

Here's a good website to demonstrate.

Again, there may be a method to do this without, code behind, but I am not aware of it. Hope it helps, let me know if I can be of any other use!

Thanks but still confused, you did not reply my exact quetions. let me clarify it.

When the page loads, both drowdownlists show all contents from country and city columns of the database table. I mean, all the data already includeded, as "All" in the dropdownlist.

When usser selects a Country, how will I do hanle it in behind code ?

Make a query to populate city dropdownlist list from a tha database where country equals the selected country (this means search all table again which is has thousadn of records)

Or, just make a query on the gridview (remember all the data called before)
If so, How can I make query on grid view ?

Doing a query through a database with a few thousand records is nothing. A database is there to store and search, so use it. Fill the country dropdown with all the countries available in the DB, once you select a country populate the cities dropdown based on the country selected.

Also if you have the data in a DataTable object you can query the DataTable the same way.

Sorry that I missed that, but yes as stbuchok mentioned, a query of thousands of records is really nothing. A decent, not even great query should run in a second or so.

That will really be the least of your overhead time, when you get into the millions of records is when your queries start to matter and run time of queries affects performance.

Moral of the story, don't be concerned with a simple database query or two.

Thank you all.
At this point, I want ot ask another question. I am using access database.

And I am confusedwant to make all operations which operations to handle qith a query, and which operations handle with filtering.

Actually I want to load all database for at once bcos it will be big, and load it once and use filters..

On the other side,
for filtering for every dropdownlist selection I need to query db again,
is it realy performance or I miss something here.

Doing it that way is a waste. What happens if your DB gets to be 500MB, are you going to load all 500MB into memory. NO FOR THE LOVE OF GOD, DON'T DO IT THIS WAY!!!!!!!!!!
The performance gain you will get will be so insignificant that it is completely not worth doing it that way. Please, please, please, just query the database each time. Each query will be considerably less than a second. Also look into using a real database and try to get out of using Access. MS SQL Express is free and considerably more powerful, hell you can even use MySQL.

OK stbuchok, pelase do not get angry with me, I will follow the way you adviced.

The problem is that I am new to ASP NEt and c#, and I am still confused at some points

if you will handle seleciton in behin the code,
then I need to write all db connection codes in the behind code right, not in ASp right ?

I mean I need to write whole db code in c # and bypass the ASP NET..

Can you help me doing it ? I know how to connect to db in c# in form application, but I never connected to db and used dropdownlist from ASP NET in c# for an ASP NET site ?

What is the way I need to follow ? Can you provide a little code sample for this. Thanks in advance.


Please take a look at the website I linked to above. It is a basic tutorial on populating a dropdown with a database connection and should answer most of your questions. Assuming you're partially familiar with queries and such, it should not be difficult to adapt to your specific needs.

As stbuchok mentioned, use of a more powerful database would be a good idea.I have used SQL Express many times and like it a lot.

Any further questions should probably go in a new thread if you can't find the answer yourself.

Hi, Thanks a lot for useful advices and links.
stbuchok, the zip file in the site you have sent its link, does not contain a working example, I think something is corrupt in that.And Croker10, there is no link in your post ??

Please I have a final question on this issue,
Actually I have a full working example with filtering (which uses no behind code)
and I want to update is.

I heard that it is possible to use ASP.NET form elements.

What Iwant to do is;
Remember the first code I have post
code for sql query;
<asp:AccessDataSource ID="dsPopulateCity" runat="server" DataFile="Database2.mdb" SelectCommand="SELECT DISTINCT City FROM T_Employees"></asp:AccessDataSource>
I wanto to repopulate dropdownlist from this query,

code for populationg dropdownlist

<asp:DropDownList ID="ddlLocation" runat="server" AppendDataBoundItems="true"
AutoPostBack="true" DataSourceID="dsPopulateLocation" DataValueField="location" ...

Here is the query I want to get worked when a Country selected from the country dropdownlist;

SelectCommand="SELECT DISTINCT City FROM T_Employees WHERE City='selectedCountry'">

I mean, in behind code event handler, I wanto to recall those sql query and populating ASP NET code, and populate my city dropdownlist with new query.
(and query will take selected valua of country dropdownlist)

Can anyone can provide a simple code for it ? Sory for taking your time. Thanks..