Hi kankon,
You need to bind OnSelectedIndexChanged Event for cities,states,country with BindGirdView.Call BindGirdView method for OnSelectedIndexChanged event.
Please refer below sample.
HTML
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table dir="rtl">
<tr>
<td style="border: medium solid #000000">القطاع<br />
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Changed">
</asp:DropDownList>
</strong></td>
<td style="border: medium solid #000000">الادارة<strong><br />
<asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="true" OnSelectedIndexChanged="State_Changed">
</asp:DropDownList>
</strong></td>
<td style="border: medium solid #000000">القسم<strong><br />
<asp:DropDownList ID="ddlCities" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Cities_Changed">
</asp:DropDownList>
</strong>
</td>
</tr>
</table>
<hr />
<asp:GridView runat="server" ID="GridView1"></asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Namespace
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "select CountryId,CountryName from Countries";
this.BindDropDownList(ddlCountries, query, "CountryName", "CountryId", "Select Country");
ddlStates.Enabled = false;
ddlCities.Enabled = false;
ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
this.BindGridView();
}
}
protected void Country_Changed(object sender, EventArgs e)
{
ddlStates.Enabled = false;
ddlCities.Enabled = false;
ddlStates.Items.Clear();
ddlCities.Items.Clear();
ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
int countryId = int.Parse(ddlCountries.SelectedItem.Value);
if (countryId > 0)
{
string query = string.Format("select StateId, StateName from States where CountryId = {0}", countryId);
this.BindDropDownList(ddlStates, query, "StateName", "StateId", "Select State");
ddlStates.Enabled = true;
}
this.BindGridView();
}
protected void State_Changed(object sender, EventArgs e)
{
ddlCities.Enabled = false;
ddlCities.Items.Clear();
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
int stateId = int.Parse(ddlStates.SelectedItem.Value);
if (stateId > 0)
{
string query = string.Format("select CityId, CityName from Cities where StateId = {0}", stateId);
this.BindDropDownList(ddlCities, query, "CityName", "CityId", "Select City");
ddlCities.Enabled = true;
}
this.BindGridView();
}
protected void Cities_Changed(object sender, EventArgs e)
{
this.BindGridView();
}
private void BindGridView()
{
string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT * FROM Customers WHERE (Country = @Country OR @Country IS NULL)";
query += " AND (Address = @Address OR @Address IS NULL) AND (City = @City OR @City IS NULL)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedIndex > 0 ? ddlCountries.SelectedItem.Text : (object)DBNull.Value);
cmd.Parameters.AddWithValue("@Address", ddlStates.SelectedIndex > 0 ? ddlStates.SelectedItem.Text : (object)DBNull.Value);
cmd.Parameters.AddWithValue("@City", ddlCities.SelectedIndex > 0 ? ddlCities.SelectedItem.Text : (object)DBNull.Value);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim query As String = "select CountryId,CountryName from Countries"
Me.BindDropDownList(ddlCountries, query, "CountryName", "CountryId", "Select Country")
ddlStates.Enabled = False
ddlCities.Enabled = False
ddlStates.Items.Insert(0, New ListItem("Select State", "0"))
ddlCities.Items.Insert(0, New ListItem("Select City", "0"))
Me.BindGridView()
End If
End Sub
Protected Sub Country_Changed(ByVal sender As Object, ByVal e As EventArgs)
ddlStates.Enabled = False
ddlCities.Enabled = False
ddlStates.Items.Clear()
ddlCities.Items.Clear()
ddlStates.Items.Insert(0, New ListItem("Select State", "0"))
ddlCities.Items.Insert(0, New ListItem("Select City", "0"))
Dim countryId As Integer = Integer.Parse(ddlCountries.SelectedItem.Value)
If countryId > 0 Then
Dim query As String = String.Format("select StateId, StateName from States where CountryId = {0}", countryId)
Me.BindDropDownList(ddlStates, query, "StateName", "StateId", "Select State")
ddlStates.Enabled = True
End If
Me.BindGridView()
End Sub
Protected Sub State_Changed(ByVal sender As Object, ByVal e As EventArgs)
ddlCities.Enabled = False
ddlCities.Items.Clear()
ddlCities.Items.Insert(0, New ListItem("Select City", "0"))
Dim stateId As Integer = Integer.Parse(ddlStates.SelectedItem.Value)
If stateId > 0 Then
Dim query As String = String.Format("select CityId, CityName from Cities where StateId = {0}", stateId)
Me.BindDropDownList(ddlCities, query, "CityName", "CityId", "Select City")
ddlCities.Enabled = True
End If
Me.BindGridView()
End Sub
Protected Sub Cities_Changed(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGridView()
End Sub
Private Sub BindGridView()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr1").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim query As String = "SELECT * FROM Customers WHERE (Country = @Country OR @Country IS NULL)"
query += " AND (State = @State OR @State IS NULL) AND (City = @City OR @City IS NULL)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Country", If(ddlCountries.SelectedIndex > 0, ddlCountries.SelectedItem.Text, CObj(DBNull.Value)))
cmd.Parameters.AddWithValue("@State", If(ddlStates.SelectedIndex > 0, ddlStates.SelectedItem.Text, CObj(DBNull.Value)))
cmd.Parameters.AddWithValue("@City", If(ddlCities.SelectedIndex > 0, ddlCities.SelectedItem.Text, CObj(DBNull.Value)))
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Private Sub BindDropDownList(ByVal ddl As DropDownList, ByVal query As String, ByVal text As String, ByVal value As String, ByVal defaultText As String)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
ddl.DataSource = cmd.ExecuteReader()
ddl.DataTextField = text
ddl.DataValueField = value
ddl.DataBind()
con.Close()
End Using
End Using
ddl.Items.Insert(0, New ListItem(defaultText, "0"))
End Sub
Screenshot