Monday, February 16, 2009

Simple Way to Use Cascading DropdownList

How to Use the Cascading Dropdown ? When You need Such type of requirment.Then Please follow the following Steps then You will find that it's very sipmle.
Step 1.) Adding Webservice to bind the Drop DownLIst
A.) Go to Website -> Add New Item ->Choose WebService ->Rename it as you want
(For Example here we have Taken the name of WebService "CascadingDataService.asmx")
B.) Write the CascadingDataService.cs code in App_Code folder as Given Below:-
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
///
/// Summary description for CascadingDataService
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class CascadingDataService : System.Web.Services.WebService
{
string conString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
public CascadingDataService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownCountries(string knownCategoryValues, string category)
{
SqlConnection sqlConn = new SqlConnection(conString);
sqlConn.Open();
SqlCommand sqlSelect = new SqlCommand("select * from country order by rowid ", sqlConn);
sqlSelect.CommandType = System.Data.CommandType.Text;
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
DataSet myDataset = new DataSet();
sqlAdapter.Fill(myDataset);
sqlConn.Close();
List cascadingValues = new List();
foreach (DataRow dRow in myDataset.Tables[0].Rows)
{
string categoryID = dRow["ID"].ToString();
string categoryName = dRow["CountryName"].ToString();
cascadingValues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(categoryName, categoryID));
}
return cascadingValues.ToArray();
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownStates(string knownCategoryValues, string category)
{
int categoryID;
StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
categoryID = Convert.ToInt32(categoryValues["category"]);
SqlConnection sqlConn = new SqlConnection(conString);
sqlConn.Open();
SqlCommand sqlSelect = new SqlCommand("select * from state where CountryID = @categoryID", sqlConn);
sqlSelect.CommandType = System.Data.CommandType.Text;
sqlSelect.Parameters.Add("@categoryID", SqlDbType.Int).Value = categoryID;
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
DataSet myDataset = new DataSet();
sqlAdapter.Fill(myDataset);
sqlConn.Close();
List cascadingValues = new List();
foreach (DataRow dRow in myDataset.Tables[0].Rows)
{
string SubcategoryID = dRow["Id"].ToString();
string SubcategoryName = dRow["StateName"].ToString();
cascadingValues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(SubcategoryName, SubcategoryID));
}
return cascadingValues.ToArray();
}
}


Step 2.) HTML page where we want to use the Cascading DropDown List :

Country : < id="ddlCountry" runat="server">
< id="CascadingDropDown1" runat="server" category="category" targetcontrolid="ddlCountry" prompttext="--Select Country--" loadingtext="Loading countries..." servicepath="CascadingDataService.asmx" servicemethod="GetDropDownCountries"> < /ajaxToolkit:CascadingDropDown >
State : < id="ddlState" runat="server">
< id="CascadingDropDown2" runat="server" category="subcategory" targetcontrolid="ddlState" parentcontrolid="ddlCountry" prompttext="--Select State--" loadingtext="Loading states..." servicepath="CascadingDataService.asmx" servicemethod="GetDropDownProducts">
< /ajaxToolkit:CascadingDropDown >

By following the Steps described above can make easy to use the Cascading DropDown List.

Thanks:
Sanjeev Kumar

No comments: