而使用asp.net AJAX功能没有定义的错误 [英] Function not defined error while using asp.net ajax
问题描述
我试图通过以下code调用通过asp.net AJAX的Web服务
i am trying to call a web service through asp.net ajax by the following code
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页面,我的渲染
this is the aspx page i am rendering
<%@ 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;"> 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>
这是Web服务我打电话
this is the web service i am calling
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'是不确定的。这是为什么。
} the page renders fine but whenever i change the list item value, it shows JavaScript runtime error: 'HRService' is undefined. why is this.
对不起,这么长的帖子......
Sorry for such a long post ....
推荐答案
您可以可以尝试 PageMethods
,只需添加使用
和 [WebMethod的]
You can can try PageMethods
, just add the using
and the [WebMethod]
using System.Web.Services;
public static class HR
{
[WebMethod]
public static int GetEmployeeCount(string department)
{
int count = 0;
...
return count;
}
}
在你的aspx修改您的ScriptManager像这样
In your aspx modify your scriptManager like this
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
然后就可以在JS调用的方法,这种方法
then you can call the method in the JS this way
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); }
}
这篇关于而使用asp.net AJAX功能没有定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!