簡體   English   中英

使用asp.net Ajax時函數未定義錯誤

[英]Function not defined error while using asp.net ajax

我試圖通過以下代碼通過asp.net ajax調用Web服務

namespace MCTS70515AJAX
{
public static class HR
{
    public static int GetEmployeeCount(string department)
    {
        int count = 0;
        switch (department)
        {
            case "Sales":
                count = 10;
                break;
            case "Engineering":
                count = 28;
                break;
            case "Marketing":
                count = 44;
                break;
            case "HR":
                count = 7;
                break;
            default:
                break;
        }
        return count;
    }
}

這是我正在渲染的aspx頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AJAX2.aspx.cs" 

Inherits="MCTS70515AJAX.AJAX2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>

</head>
<body>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="HRSer.asmx" />
            </Services>
            <Scripts>

            </Scripts>
        </asp:ScriptManager>


        <div>
            <select id="Departments" size="5">
                <option value="Engineering">Engineering</option>
                <option value="HR">Human Resources</option>
                <option value="Sales">Sales</option>
                <option value="Marketing">Marketing</option>

            </select>

        </div>
        <br />
        <div>
            <span id="employeeResults"></span>
            <span id="loading" style="display: none;">&nbsp;&nbsp;Loading ... 

            </span>

        </div>

    </form>
     <script type="text/javascript">

    var departments = null;
    Sys.Application.add_load(page_load);
    Sys.Application.add_unload(page_unload);
    function page_load(sender, e) {
        departments = $get("Departments");
        $addHandler(departments, "change", departments_onchange);
    }
    function page_unload(sender, e) {
        $removeHandler(departments, "change", departments_onchange);
    }
    function departments_onchange(sender, e) {
        $get("employeeResults").innerHTML = ""; $get("loading").style.display = "block";
        var selectedValue = departments.value;
        HRService.Getcount(selectedValue, onSuccess);
    }
    function onSuccess(result) {
        $get("loading").style.display = "none";
        $get("employeeResults").innerHTML = "Employee count: " + result;
    }


                </script>

</body>
</html>

這是我正在呼叫的網絡服務

namespace MCTS70515AJAX
{

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class HRService : System.Web.Services.WebService
{
    [ScriptMethod]
    [WebMethod]
    public int Getcount(string department)
    {
        return HR.GetEmployeeCount(department);
    }
}

頁面呈現得很好,但是每當我更改列表項的值時,它就會顯示JavaScript運行時錯誤:'HRService'未定義。 為什么是這樣。

抱歉這么長的帖子....

您可以嘗試PageMethods ,只需添加using[WebMethod]

using System.Web.Services;

public static class HR
    {
        [WebMethod]
        public static int GetEmployeeCount(string department)
        {
            int count = 0;
            ...
            return count;
        }
    }

在你的aspx中像這樣修改你的scriptManager

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

然后您可以通過這種方式在JS中調用該方法

function myJS_method() {
    var departments = $get("Departments"); // your string value
    PageMethods.GetEmployeeCount(departments , onSucess, onError);
    function onSucess(result) {
        // your code when it is OK
        // result is the return value of your C# method
    }
    function onError(result) { alert('Error' + result); }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM