Cascading Dropdownlist using webservices

Here is Html
 <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <div>
        <table style="width: 100%;">
            <tr>
                <td colspan="3" class="style2">
                    <h1 align="center">
                        Cascading Dropdown List Style
                    </h1>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Selecte Country&nbsp;:-
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    </asp:DropDownList>
                    <asp:CascadingDropDown ID="CascadingDropDown4" runat="server" TargetControlID="DropDownList1"
                        LoadingText="Loding Country" Category="Country" PromptText="Selecte Country"
                        ServiceMethod="Getcountry" ServicePath="~/WebService.asmx">
                    </asp:CascadingDropDown>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Selecte State:-
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                    </asp:DropDownList>
                    <asp:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
                        ParentControlID="DropDownList1" Category="State" LoadingText="Loading State"
                        PromptText="Selecte State" ServiceMethod="Getstate" ServicePath="~/WebService.asmx">
                    </asp:CascadingDropDown>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Selecte City:-
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList3" runat="server">
                    </asp:DropDownList>
                    <asp:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
                        ParentControlID="DropDownList2" PromptText="Selecte City" Category="City" LoadingText="Loading City"
                        ServiceMethod="Getcity" ServicePath="~/WebService.asmx">
                    </asp:CascadingDropDown>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>
    </form>




WebService.cs  below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using AjaxControlToolkit;
using System.Collections.Specialized;

/// <summary>
/// Summary description for WebService
/// </summary>
[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 WebService : System.Web.Services.WebService
{

    public WebService()
    {

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

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public static CascadingDropDownNameValue[] Getcountry(string knownCatagoryValues, string catagory)
    {
        String str1 = "Select * from country_master";
        DataTable dt = new DataTable();
        bal b = new bal();
        dt = b.bind(str1);
        List<CascadingDropDownNameValue> oq = new List<CascadingDropDownNameValue>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            oq.Add(new CascadingDropDownNameValue(dt.Rows[i]["country_name"].ToString(), dt.Rows[i]["country_id"].ToString()));
        }
        return oq.ToArray();
    }
    [System.Web.Services.WebMethod]

    public static CascadingDropDownNameValue[] Getstate(string knownCatagoryValues, string catagory)
    {
        StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCatagoryValues);
        int counrtid = Convert.ToInt32(countrydetails["Country"]);

        String str2 = "Select * from state_master where country_id =" + counrtid + "";
        DataTable ddt = new DataTable();
        bal b = new bal();
        ddt = b.bind(str2);
        List<CascadingDropDownNameValue> st = new List<CascadingDropDownNameValue>();
        for (int i = 0; i < ddt.Rows.Count; i++)
        {
            st.Add(new CascadingDropDownNameValue(ddt.Rows[i]["state_name"].ToString(), ddt.Rows[i]["country_id"].ToString()));

        }
        return st.ToArray();
    }
    [System.Web.Services.WebMethod]

    public static CascadingDropDownNameValue[] Getcity(string knownCatagoryValues, string catagory)
    {
        StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCatagoryValues);
        int Sid = Convert.ToInt32(countrydetails["State"]);

        String str3 = "Select * from city_master where state_id=" + Sid + "";
        DataTable zdt = new DataTable();
        bal b = new bal();
        zdt = b.bind(str3);
        List<CascadingDropDownNameValue> ct = new List<CascadingDropDownNameValue>();
        for (int i = 0; i < zdt.Rows.Count; i++)
        {
            ct.Add(new CascadingDropDownNameValue(zdt.Rows[i]["city_name"].ToString(), zdt.Rows[i]["state_id"].ToString()));

        }
        return ct.ToArray();
    }
}











Comments

Post a Comment

Popular posts from this blog

Private Chat Using Node js and socket.io with encrypt and decrypt message and insert into mongo db

How To Migrate MVC 3 Application To MVC 5

Populate a drop-down in Vue.js and Asp.net Core from an ajax call