I'm trying to run a webservice from my web page and I'm having a little problem. The webservice is used to populate a second dropdownlist. When an item is selected from the first dropdownlist it calls GetTerritories.
GetTerritories calls the web service TerritoriesService.GetTerritoriesInRegion.
But I’m getting an error specifying "TerritoriesService not defined". It appears that the Javascript function can’t find TerritoriesService. Any ideas as to what’s missing?
I enclosed the web service below.
<asp:DropDownList ID="lstRegions" Runat="server" Width="210px"
DataTextField="label_type"
DataValueField="label_type_id"
onChange="GetTerritories(this.value);">
<asp:DropDownList ID="lstTerritories" Runat="server" Width="275px">
function GetTerritories(label_type_id) {
TerritoriesService.GetTerritoriesInRegion(label_type_id,
OnRequestComplete, OnError);
}
using System;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class TerritoriesService : System.Web.Services.WebService
{
[WebMethod()]
public List<territory> GetTerritoriesInRegion(int label_type_id)
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["Engineering"].ConnectionString);
SqlCommand cmd = new SqlCommand(
"SELECT * FROM tblMLPLabelDescription WHERE label_type_id=@label_type_id", con);
cmd.Parameters.Add(new SqlParameter("@label_type_id", SqlDbType.Int, 4));
cmd.Parameters["@label_type_id"].Value = label_type_id;
List<territory> territories = new List<territory>();
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
territories.Add(new Territory(
reader["TerritoryID"].ToString(),
reader["TerritoryDescription"].ToString()));
}
reader.Close();
}
catch (SqlException ex)
{
throw new ApplicationException("Data error.");
}
finally
{
con.Close();
}
return territories;
}
}
public class Territory
{
public string ID;
public string Description;
public Territory(string id, string description)
{
this.ID = id;
this.Description = description;
}
public Territory() { }
}