简体   繁体   English

在用户控制下注册或使用 web 服务,而不是母版页

[英]Register or use web service at user control, rather than master page

Just trying to see if I can add a definition/register AJAX web service explicitly in a user control that uses it (via client side).只是想看看我是否可以在使用它的用户控件中显式添加定义/注册 AJAX web 服务(通过客户端)。 Web service is defined and located on the same server as the control(s) that use it, but the control is unable to use it. Web 服务已定义并位于与使用它的控件相同的服务器上,但控件无法使用它。

It works when I'm adding reference to the web service on SriptManager on the master page, but I want to specifically add it to be used by custom control, and then wan't to remove it from the master page.当我在母版页上的 SriptManager 上添加对 web 服务的引用时,它可以工作,但我想专门添加它以供自定义控件使用,然后不想从母版页中删除它。

Scenario: I've tried: <%@ Register TagPrefix="ajaxws" Assembly="AssemblyName" Namespace="WebService.AjaxNameSpace" %>场景:我试过:<%@ Register TagPrefix="ajaxws" Assembly="AssemblyName" Namespace="WebService.AjaxNameSpace" %>

Let me know if some more details are needed.让我知道是否需要更多细节。 I've been researching a fix for this, but the only reliable fix I found, is to add the.asmx to ScriptManager on a master page.我一直在研究解决这个问题,但我发现的唯一可靠的解决方法是将.asmx 添加到母版页上的 ScriptManager。

It would seem you are trying to add a script reference or a service reference to your script manager.您似乎正在尝试向脚本管理器添加脚本引用或服务引用。

You can accomplish this in the code-behind of your user control, using a statement such as:您可以在用户控件的代码隐藏中完成此操作,使用如下语句:

ScriptManager.GetCurrent(this.Page).Scripts.Add(/* Whatever it is you want to add */);

... for which you need the Script Reference class, or... ...您需要脚本参考class 或...

ScriptManager.GetCurrent(this.Page).Services.Add(/* Whatever it is you want to add */);

... for which you need the Service Reference class. ...您需要服务参考class。

Note that either approach will give you fits if the page doesn't have a script manager.请注意,如果页面没有脚本管理器,这两种方法都适合您。

If you are able to use jquery in your solution and you want to work the reference completely from the client side.如果您能够在您的解决方案中使用 jquery 并且您希望完全从客户端工作参考。 You can do this:你可以这样做:

<script type="text/javascript">
    function CallService() {
        $.ajax({
            type: "POST", //You can use POST or GET (GET works a little different)
            url: "YourWebService.asmx/GetSomeData", //This assumes a relative path, you can use any url.
            data: "{}",
            contentType: "application/json; charset=utf-8", // you must define the content type (though I am not sure what other options are of any value here...)
            dataType: "json", //you can do json or XML serialization, do json, it is easier in the long run.
            success: Success, //this is a reference to the "function Success" to be invoke by the the service return.
            error: Error //ditto for the function Error...
        });
    }

    function Success(data, status) {
        //do what you want with the data...
    }

    function Error(request, status, error) {
        //something in the soup ain't right...
    }
</script>

This is really about as easy as it gets for invoking a web service using ajax and jQuery.这实际上与使用 ajax 和 jQuery 调用 web 服务一样简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM