Monday, 27 February 2012

Ajax Cascading Dropdownlist using webservice,linq in asp.net

DESIGN THE CODE LIKE THIS:
<form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
    <table align="center">
    <tr><td>COUNTRY</td><td><asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>&nbsp;<asp:CascadingDropDown ID="CascadingDropDown1" Category="COUNTRY" runat="server" TargetControlID="ddl" ServiceMethod="x" ServicePath="~/WebService2.asmx" PromptText="Select">
        </asp:CascadingDropDown>
    </td></tr>
    <tr><td>STATE</td><td><asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>&nbsp;<asp:CascadingDropDown ID="cs" runat="server" Category="STATE" TargetControlID="ddl1" ParentControlID="ddl" PromptText="Select" ServicePath="~/WebService2.asmx" ServiceMethod="yy"></asp:CascadingDropDown></td></tr>
    </table>
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </form>

WEBSERVICE CODE :

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using AjaxControlToolkit;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class WebService2 : System.Web.Services.WebService
{
    DataClassesDataContext o = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString());
    int i;
    string b, c;
    List<CascadingDropDownNameValue> p = new List<CascadingDropDownNameValue>();
    [WebMethod]
    public CascadingDropDownNameValue[] x()
    {
        var m = from a in o.COUNTRies select a;
        foreach (var n in m)
        {
            b =Convert.ToString( n.CID);
            c = n.CNAME;
            p.Add(new CascadingDropDownNameValue(c, b));
        }
        return p.ToArray();
    }
    [WebMethod]
    public CascadingDropDownNameValue[] yy(string knownCategoryValues)
    {
        StringDictionary sd = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        i = Convert.ToInt32(sd["COUNTRY"]);
        var n = from d in o.STATEs where d.CID == i select d;
        foreach (var m in n)
        {
            b = Convert.ToString(m.SID);
            c = m.SNAME;
            p.Add(new CascadingDropDownNameValue(c, b));
        }
        return p.ToArray();
    }
  
  
   
}

No comments:

Post a Comment