2012-05-30 62 views
0

我想填充使用AJAX的下拉列表,我一直在得到方法錯誤500.我花了一整天的時間嘗試所有在幾個論壇上找到的東西,但似乎沒有任何幫助。使用AJAX方法填充DropDownList錯誤500

如果任何人都可以幫助我,那會很棒。謝謝!

ddl.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ddl.aspx.cs" Inherits="TSS.Administrator.ddl" %> 

<%@ Register TagPrefix="ajax" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %> 
<!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></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager runat="server"> 
    </asp:ScriptManager> 
    <div> 
     <asp:DropDownList ID="ddlCourseLevel" runat="server"> 
     </asp:DropDownList> 
     <ajax:CascadingDropDown ID="ccdCourseLevel" runat="server" Category="CourseLevel" 
      TargetControlID="ddlCourseLevel" PromptText="---Select CourseLevel" LoadingText="Loading Countries.." 
      ServiceMethod="BC" ServicePath="~/WebServices/ws1.asmx"> 
     </ajax:CascadingDropDown> 
    </div> 
    </form> 
</body> 
</html> 

ws1.asmx

<%@ WebService Language="C#" CodeBehind="ws1.asmx.cs" Class="TSS.ws1" %> 

ws1.asmx.cs

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

namespace TSS 
{ 
    /// <summary> 
    /// Summary description for ws1 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // 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 ws1 : System.Web.Services.WebService 
    { 

     dbConnection dbConn = new dbConnection(); 

     [WebMethod] 
     public CascadingDropDownNameValue[] BC(string knownCategoryValues, string category) 
     { 

      DataTable dt = new DataTable(); 

      //CourseLevelDAL _courseLevelDAL = new CourseLevelDAL(); 

      dt = Admin_Course_WebService.PopulateCourseLevel(); 

      //create list and add items in it by looping through dataset table 
      List<CascadingDropDownNameValue> courseleveldetails = new List<CascadingDropDownNameValue>(); 
      foreach (DataRow dtrow in dt.Rows) 
      { 
       string Name = dtrow["Name"].ToString(); 
       string Id = dtrow["Id"].ToString(); 
       courseleveldetails.Add(new CascadingDropDownNameValue(Name, Id)); 
      } 
      return courseleveldetails.ToArray(); 
     } 
    } 
} 

回答

0

我使用的調試工具提琴手,發現它找不到Web方法BC和它無法找到它的原因是因爲它是一個靜態方法。我將它改爲public並添加了屬性[System.Web.Script.Services.ScriptService]。有效!