Introduction
In this code snippet, I have shared the real time code which was working fine in my project to fill the country state and city based on the dropdown list items selected . Attached the source code in attachment section.
Aspx code
<table>
<tr>
<td>
SelectCountry
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" Width="100px">
</asp:DropDownList>
<ap:CascadingDropDown ID="CountryCascading" runat="server" Category="Country" TargetControlID="ddlCountry" LoadingText="Loading Countries..." PromptText="Select Country" ServiceMethod="BindCountry" ServicePath="~/Address_WebService.asmx">
</ap:CascadingDropDown>
</td>
</tr>
<tr>
<td>
SelectState
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" Width="100px">
</asp:DropDownList>
<ap:CascadingDropDown ID="StateCascading" runat="server" Category="State" TargetControlID="ddlState" ParentControlID="ddlCountry" LoadingText="Loading States..." PromptText="Select State" ServiceMethod="BindState" ServicePath="~/Address_WebService.asmx"></ap:CascadingDropDown>
</td>
</tr>
<tr>
<td>
SelectCity
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" Width="100px">
</asp:DropDownList>
<ap:CascadingDropDown ID="CityCascading" runat="server" Category="Region" TargetControlID="ddlCity" ParentControlID="ddlState" LoadingText="Loading Cities..." PromptText="selectCity" ServiceMethod="BindCity" ServicePath="~/Address_WebService.asmx"> </ap:CascadingDropDown>
-
</td>
</tr>
</table>Web service methods, [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Address_WebService: System.Web.Services.WebService
{
SqlConnection conn = new SqlConnection(OneStopMethods_Common.constring_Property);
[WebMethod]
public CascadingDropDownNameValue[] BindCountry(string knownCategoryValues, string category)
{
DataSet ds = new DataSet();
conn.Open();
SqlCommand cmd = new SqlCommand("select * from SelectCountry", conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
adp.Fill(ds);
conn.Close();
List < CascadingDropDownNameValue > CountryDetails = new List < CascadingDropDownNameValue > ();
foreach(DataRow DR in ds.Tables[0].Rows)
{
string CountryID = DR["CountryId"].ToString();
string CountryName = DR["County"].ToString();
CountryDetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
}
return CountryDetails.ToArray();
}
//Web method for bind state
[WebMethod]
public CascadingDropDownNameValue[] BindState(string knownCategoryValues, string category)
{
DataSet ds = new DataSet();
int CountryID;
StringDictionary CountryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
CountryID = Convert.ToInt32(CountryDetails["Country"]);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from SelectCountrySelectState where CountryId=@CountryID", conn);
cmd.Parameters.AddWithValue("@CountryId", CountryID);
cmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
conn.Close();
List < CascadingDropDownNameValue > StateDetails = new List < CascadingDropDownNameValue > ();
foreach(DataRow DR in ds.Tables[0].Rows)
{
string stateID = DR["StateId"].ToString();
string statename = DR["State"].ToString();
StateDetails.Add(new CascadingDropDownNameValue(statename, stateID));
}
return StateDetails.ToArray();
}
//Web method for bind city
[WebMethod]
public CascadingDropDownNameValue[] BindCity(string knownCategoryValues, string category)
{
DataSet ds = new DataSet();
int stateID;
StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
stateID = Convert.ToInt32(statedetails["State"]);
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from SelectStateSelectCity where StateId= " + stateID, conn);
// cmd.Parameters.AddWithValue("@StateID", StateId)
cmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
conn.Close();
List < CascadingDropDownNameValue > CityDetails = new List < CascadingDropDownNameValue > ();
foreach(DataRow DR in ds.Tables[0].Rows)
{
string CityID = DR["CityID"].ToString();
string City = DR["City"].ToString();
CityDetails.Add(new CascadingDropDownNameValue(City, CityID));
}
return CityDetails.ToArray();
}
}