Sunday, July 1, 2012

ASP.NET(Cascading Dropdown with WEBService)


//NOTE: This Example will help you to populate dropdowns without postback of the page working together like one combo populates the countries and other populate the relivent cites of it, and you will require the ajax control toolkit to do that you also have to make a service locally.


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


//Step01: Country DropDown       
<asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>


//Step02: City DropDown
<
asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>


//Step03: Use Cascading Dropdown with the appropriate ASP dropdown controls
<asp:CascadingDropDown ID="CDD_Country" runat="server"
                TargetControlID="ddlCountry"
                Category="Country"
                PromptText="Select a Country"
                LoadingText="Please wait..."
                ServicePath="~/ServiceName.asmx"
                ServiceMethod="MethodForPopulatingCountriesHere"/>

        <asp:CascadingDropDown ID="CDD_City" runat="server"
                TargetControlID="ddlCity"
                ParentControlID="ddlCountry"
                Category="Country"
                PromptText="Select a City"
                LoadingText="Please wait..."
                ServicePath="~/ServiceName.asmx"
                ServiceMethod="MethodForPopulatingCitiesHere"/>
  

//Step04: WEBService That’s Populating Cities and Countries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AjaxControlToolkit;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections.Specialized;

/// <summary>
/// Summary description for Location
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
// 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 Location : System.Web.Services.WebService {

    public Location () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetCountry(
       string knownCategoryValues, string category)
    {
        string strSql = "SELECT DISTINCT CountryName FROM TableName";
        string strError = string.Empty;

        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["uportal_cnn"].ConnectionString;
        cn.Open();

        DataSet ds = new DataSet();
        SqlDataAdapter sdapt = new SqlDataAdapter(strSql, cn);

        sdapt.Fill(ds);

        List<CascadingDropDownNameValue> cascadingValues = new List<CascadingDropDownNameValue>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            cascadingValues.Add(new CascadingDropDownNameValue()
            {
                name = ds.Tables[0].Rows[i][0].ToString(),
                value = ds.Tables[0].Rows[i][0].ToString()
            });
        }
       

        return cascadingValues.ToArray();
    }


    [WebMethod]
    public CascadingDropDownNameValue[] GetCities(
       string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        string country = string.Empty;

        if (!kv.ContainsKey("Country"))
        {
            return null;
        }

        country = kv["Country"].ToString();


        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["uportal_cnn"].ConnectionString;
        cn.Open();


        string strSql = "SELECT CityName FROM Table Name WHERE CountryName = @pm_country ";
        SqlCommand command = new SqlCommand(strSql, cn);
        command.Parameters.AddWithValue("@pm_country", country);
        string strError = string.Empty;

        DataSet ds = new DataSet();
        SqlDataAdapter sdapt = new SqlDataAdapter(command);

        sdapt.Fill(ds);

        List<CascadingDropDownNameValue> cascadingValues = new List<CascadingDropDownNameValue>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            cascadingValues.Add(new CascadingDropDownNameValue()
            {
                name = ds.Tables[0].Rows[i][0].ToString(),
                value = ds.Tables[0].Rows[i][0].ToString()
            });
        }


        return cascadingValues.ToArray();
    }
}


No comments:

Post a Comment