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

本文关键字:服务 母版页 web 控制 用户 注册 | 更新日期: 2023-09-27 17:53:54

只是想看看我是否可以在使用它的用户控件中显式地添加定义/注册AJAX web服务(通过客户端)。Web服务与使用它的控件定义并位于同一服务器上,但是控件无法使用它。

当我在母版页上添加对SriptManager上web服务的引用时,它可以工作,但我想特别添加它以供自定义控件使用,然后不想从母版页中删除它。

场景:我试过了:<%@ Register TagPrefix="ajaxws" Assembly="AssemblyName" Namespace="WebService. net "AjaxNameSpace " %>

如果需要更多的细节,请告诉我。我一直在研究解决这个问题的方法,但我找到的唯一可靠的方法是在母版页上将.asmx添加到ScriptManager中。

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

您似乎正在尝试向脚本管理器添加脚本引用或服务引用。

您可以在用户控件的后台代码中完成此操作,使用如下语句:

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

…需要Script Reference类,或者…

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

请注意,如果页面没有脚本管理器,这两种方法都会给您带来麻烦。

如果您能够在您的解决方案中使用jquery,并且您希望完全从客户端工作引用。你可以这样做:

<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>

使用ajax和jQuery调用web服务真的很简单。