使用 AJAX 方法填充下拉列表错误 500

本文关键字:错误 下拉列表 填充 AJAX 方法 使用 | 更新日期: 2023-09-27 18:35:53

我正在尝试使用 AJAX 填充下拉列表,但一直收到方法错误 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();
        }
    }
}

使用 AJAX 方法填充下拉列表错误 500

我使用调试工具Fiddler,发现它找不到Web方法BC,找不到它的原因是因为它是一个静态方法。我将其更改为公共并添加了属性[System.Web.Script.Services.ScriptService]。成功了!