Hi
try this example to create AJAX Cascade DropdownList with Linq :-
Step1 :-
Open SQL Server 2005 and create new database and create the following Tables :-
a) Country table :-
- Id –> int (Primary Key ) and Auto increment
- Name –> nvarchar(20)
b) City table :-
- Id –> int (Primary Key ) and Auto increment
- Name –> nvarchar(20)
- Country_Id –> int (foreign key)
Step 2 :-
Open Visual Studio 2008 and create new website and add new DataContext class called “DataClasses.dbml”
and drag Country and City table inside it
Step 3 :-
Add new Web Service called “WebService” and in WebServive.cs add the following code :-
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Collections.Generic;
using System.Text.RegularExpressions;
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 CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category)
{
DataClassesDataContext db = new DataClassesDataContext();
IEnumerable<CascadingDropDownNameValue> vals = null;
short targetID = 0;
vals = from tp in db.Countries
select new CascadingDropDownNameValue
{
name = tp.Name,
value = tp.Id.ToString(),
};
return vals.ToArray<CascadingDropDownNameValue>();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCities(string knownCategoryValues, string category)
{
DataClassesDataContext db = new DataClassesDataContext();
IEnumerable<CascadingDropDownNameValue> vals = null;
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
short country_id;
if (!kv.ContainsKey("Country") || !short.TryParse(kv["Country"], out country_id))
{
return null;
}
vals = from tp in db.Cities
where tp.Country_Id == country_id
select new CascadingDropDownNameValue
{
name = tp.Name,
value = tp.Id.ToString(),
};
return vals.ToArray<CascadingDropDownNameValue>();
}
}
Step 4 :-
Add new web page and script Manager control and two DropdownList control and two AJAX CascadingDropDown
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table style="width: 50%;">
<tr>
<td>
Select Country :-</td>
<td>
<asp:DropDownList ID="ddl_Country" runat="server">
</asp:DropDownList>
<cc1:CascadingDropDown ID="ddl_Country_CascadingDropDown" runat="server"
Enabled="True"
TargetControlID="ddl_Country"
LoadingText="Loding ..."
PromptText="Select Country"
ServiceMethod="GetCountries"
ServicePath="WebService.asmx"
Category="Country"
UseContextKey="True">
</cc1:CascadingDropDown>
</td>
</tr>
<tr>
<td>
Select City :-</td>
<td>
<asp:DropDownList ID="ddl_City" runat="server">
</asp:DropDownList>
<cc1:CascadingDropDown ID="ddl_City_CascadingDropDown" runat="server"
Enabled="True"
ParentControlID="ddl_Country"
TargetControlID="ddl_City"
LoadingText="Loding ..."
PromptText="Select City"
ServiceMethod="GetCities"
ServicePath="WebService.asmx"
Category="City"
UseContextKey="True">
</cc1:CascadingDropDown>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 5 :-
In Code behind add the following code :-
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
// Country WebMethod
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] GetCountry(string knownCategoryValues, string category)
{
return new WebService().GetCountries(knownCategoryValues, category);
}
// City WebMethod
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] GetCity(string knownCategoryValues, string category)
{
return new WebService().GetCities(knownCategoryValues, category);
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Hope this helps
Good Luck.