Client
<%@ register
namespace="AjaxControlToolkit"
assembly="AjaxControlToolkit"
tagprefix="ajax"
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptmanager1" runat="server">
</asp:ScriptManager>
<div>
<table>
<tr>
<td>
Select Country:
</td>
<td>
<asp:DropDownList ID="ddlcountry" runat="server">
</asp:DropDownList>
<ajax:CascadingDropDown ID="ccdCountry"
runat="server"
Category="Country"
TargetControlID="ddlcountry"
PromptText="Select Country" LoadingText="Loading
Countries.." ServiceMethod="BindCountryDetails"
ServicePath="CascadingDropdown.asmx">
</ajax:CascadingDropDown>
</td>
</tr>
<tr>
<td>
Select State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
<ajax:CascadingDropDown ID="ccdState" runat="server" Category="State" ParentControlID="ddlcountry"
TargetControlID="ddlState" PromptText="Select
State" LoadingText="Loading
States.."
ServiceMethod="BindStateDetails" ServicePath="CascadingDropdown.asmx">
</ajax:CascadingDropDown>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Webservice
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using AjaxControlToolkit;
/// <summary>
/// Summary description for
CascadingDropdown
/// </summary>
[WebService(Namespace
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class CascadingDropdown : System.Web.Services.WebService
{
//Database connection string
private static string strconnection =
ConfigurationManager.AppSettings["ConnectionString"].ToString();
//database connection
SqlConnection
concountry = new SqlConnection(strconnection);
public CascadingDropdown () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
/// <summary>
/// WebMethod to Populate COuntry
Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[]
BindCountryDetails(string knownCategoryValues,string category)
{
concountry.Open();
SqlCommand
cmdcountry = new SqlCommand("select * from Country",
concountry);
cmdcountry.ExecuteNonQuery();
SqlDataAdapter
dacountry = new SqlDataAdapter(cmdcountry);
DataSet
dscountry = new DataSet();
dacountry.Fill(dscountry);
concountry.Close();
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue>
countrydetails = new List<CascadingDropDownNameValue>();
foreach(DataRow dtrow in
dscountry.Tables[0].Rows)
{
string CountryID = dtrow["CountryID"].ToString();
string CountryName = dtrow["CountryName"].ToString();
countrydetails.Add(new CascadingDropDownNameValue(CountryName,
CountryID));
}
return countrydetails.ToArray();
}
/// <summary>
/// WebMethod to Populate State
Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[] BindStateDetails(string knownCategoryValues,string
category)
{
int countryID;
//This method will return a StringDictionary containing the
name/value pairs of the currently selected values
StringDictionary
countrydetails
=AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
countryID
= Convert.ToInt32(countrydetails["Country"]);
concountry.Open();
SqlCommand
cmdstate = new SqlCommand("select * from State where
CountryID=@CountryID", concountry);
cmdstate.Parameters.AddWithValue("@CountryID", countryID);
cmdstate.ExecuteNonQuery();
SqlDataAdapter
dastate = new SqlDataAdapter(cmdstate);
DataSet
dsstate = new DataSet();
dastate.Fill(dsstate);
concountry.Close();
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> statedetails
= new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in
dsstate.Tables[0].Rows)
{
string StateID = dtrow["StateID"].ToString();
string StateName = dtrow["StateName"].ToString();
statedetails.Add(new CascadingDropDownNameValue(StateName, StateID));
}
return statedetails.ToArray();
}
need
to write webmethods this format only
In More Detail
No comments:
Post a Comment