Hi haider6.9,
I have created sample that full fill your requirement. Refer the below sample.
webform2.aspx
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:LinkButton Text="" ID="lnkFake" runat="server" />
<cc1:ModalPopupExtender ID="mpe" runat="server" PopupControlID="pnlPopup" TargetControlID="lnkFake"
CancelControlID="btnClose" BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
<div class="header">
Cascading DropDownList
</div>
<div>
<table>
<tr>
<td>
Country
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
<asp:ListItem Text="--Select Country--" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
State
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" Enabled="false"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
<asp:ListItem Text="--Select State--" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
City
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" Enabled="false" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="--Select City--" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</div>
<div class="footer" align="right">
<asp:Button ID="btnClose" runat="server" Text="Close" CssClass="button" />
</div>
</asp:Panel>
</div>
webform2.aspx.cs
static string choice = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDropDownList(ddlCountry, "SELECT [CountryId],[CountryName] FROM [Countries]", "CountryName", "CountryId");
mpe.Show();
}
}
private void PopulateDropDownList(DropDownList ddl, string query, string dataTextField, string dataValueField)
{
ddl.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.Connection = con;
try
{
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
choice += "Country=" + ddlCountry.SelectedItem.Text;
string query = "SELECT [StateId],[CountryId],[StateName] FROM [States] WHERE CountryId = " + ddlCountry.SelectedValue;
PopulateDropDownList(ddlState, query, "StateName", "StateId");
ddlState.Enabled = true;
mpe.Show();
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
choice += "&State=" + ddlState.SelectedItem.Text;
string query = "SELECT [CityId],[StateId],[CityName] FROM [Cities] WHERE StateId = " + ddlState.SelectedValue;
PopulateDropDownList(ddlCity, query, "CityName", "CityId");
ddlCity.Enabled = true;
mpe.Show();
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
choice += "&City=" + ddlCity.SelectedItem.Text;
Response.Redirect("webform1.aspx?" + choice);
}
webform2.aspx.vb
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
PopulateDropDownList(ddlCountry, "SELECT [CountryId],[CountryName] FROM [Countries]", "CountryName", "CountryId")
mpe.Show()
End If
End Sub
Private Sub PopulateDropDownList(ddl As DropDownList, query As String, dataTextField As String, dataValueField As String)
ddl.AppendDataBoundItems = True
Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = query
cmd.Connection = con
Try
con.Open()
ddl.DataSource = cmd.ExecuteReader()
ddl.DataTextField = dataTextField
ddl.DataValueField = dataValueField
ddl.DataBind()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub
Protected Sub ddlCountry_SelectedIndexChanged(sender As Object, e As EventArgs)
choice += "Country=" + ddlCountry.SelectedItem.Text
Dim query As String = "SELECT [StateId],[CountryId],[StateName] FROM [States] WHERE CountryId = " + ddlCountry.SelectedValue
PopulateDropDownList(ddlState, query, "StateName", "StateId")
ddlState.Enabled = True
mpe.Show()
End Sub
Protected Sub ddlState_SelectedIndexChanged(sender As Object, e As EventArgs)
choice += "&State=" + ddlState.SelectedItem.Text
Dim query As String = "SELECT [CityId],[StateId],[CityName] FROM [Cities] WHERE StateId = " + ddlState.SelectedValue
PopulateDropDownList(ddlCity, query, "CityName", "CityId")
ddlCity.Enabled = True
mpe.Show()
End Sub
Protected Sub ddlCity_SelectedIndexChanged(sender As Object, e As EventArgs)
choice += "&City=" + ddlCity.SelectedItem.Text
Response.Redirect(Convert.ToString("webform1.aspx?") & choice)
End Sub
webform1.aspx
<asp:Label ID="lblChoices" runat="server" />
webform1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["Country"]) && !string.IsNullOrEmpty(Request.QueryString["State"]) && !string.IsNullOrEmpty(Request.QueryString["City"]))
{
string country = "Country = " + Request.QueryString["Country"];
string state = "State = " + Request.QueryString["State"];
string city = "City = " + Request.QueryString["City"];
lblChoices.Text = country + "<br/>" + state + "<br/>" + city;
}
}
webform1.aspx.vb
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not String.IsNullOrEmpty(Request.QueryString("Country")) AndAlso Not String.IsNullOrEmpty(Request.QueryString("State")) AndAlso Not String.IsNullOrEmpty(Request.QueryString("City")) Then
Dim country As String = "Country = " + Request.QueryString("Country")
Dim state As String = "State = " + Request.QueryString("State")
Dim city As String = "City = " + Request.QueryString("City")
lblChoices.Text = Convert.ToString((Convert.ToString(country & Convert.ToString("<br/>")) & state) + "<br/>") & city
End If
End Sub
Screenshot
