为什么我的CallBack功能不起作用?

时间:2010-11-03 12:43:46

标签: javascript asp.net ajax web-services asp.net-ajax

我正在尝试调用WebService

我完全按照本文中的描述进行了

http://viralsarvaiya.wordpress.com/2010/03/23/calling-web-service-from-java-script-in-asp-net-c/

查看firebug的控制台我可以看到我的函数已执行并返回了预期的数据,但我的回调函数(OnComplete,OnError,OnTimeOut)从未执行过。

怎么了?

这是代码(文章的相同代码) Service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://Localhost...xys/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]

[System.Web.Script.Services.ScriptService()]

public class Service : System.Web.Services.WebService
{
    public Service () {

    //Uncomment the following line if using designed components
    //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld(string strNoOfData)
    {
        return strNoOfData;
    }
}

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
<script type="text/javascript" language="javascript">
function CallService() {
    Service.HelloWorld(document.getElementById('Textbox1').value,
        OnComplete, OnError, OnTimeOut);
}

function OnComplete(Text) {
    alert(Text);
}

function OnTimeOut(arg) {
    alert("timeOut has occured");
}

function OnError(arg) {
    alert("error has occured: " + arg._message);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/Service.asmx" />
    </Services>
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<asp:TextBox ID="Textbox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Call Service" OnClientClick="CallService()" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

我是一个VB人,大部分都是......

按顺序一次尝试一个。

首先看看你是否真的选择了文本框,我对此表示怀疑。将ClientIDMode设置为静态。

第二次尝试[WebMethod(), ScriptMethod(ResponseFormat:=ResponseFormat.Json)]

第三个使方法静态.. oops虚拟和类也。

答案 1 :(得分:1)

问题是项目类型,它适用于Web应用程序,而不适用于WebSites

相关问题