如何从母版中调用Webservice函数

本文关键字:调用 Webservice 函数 | 更新日期: 2023-09-27 18:07:56

我想在用户按功能键时打开一个html页面。

现在功能键的捕获工作正常,但是打开相应页面的逻辑是在母版页的Code-Behind中编写的。

我在网上读到,如果我想在代码后面调用函数,那么我必须创建一个web服务,并从母版页调用webservice方法。

但由于某些原因,这不起作用。可能是我没有正确调用web服务。

有人能帮忙吗?

多谢

  <cc1:ToolkitScriptManager ID="ScriptManager1" runat="server" ScriptMode="Release" EnableHistory="true" EnableSecureHistoryState="false"
                EnablePageMethods="true" CombineScripts="false">
            <Scripts>                   
                <asp:ScriptReference Path="~/ShowHelpPage.asmx"  />   
            </Scripts>
        </cc1:ToolkitScriptManager> 
document.onkeydown = function(event){
            if(window.event && window.event.keyCode == 113)
            {
                window.open("HelpFile/index.html");
            }
            else if(event.which == 113)
            {
                 window.open("HelpFile/index.html");
                 DisplayHelpFile();
            }
        }       
        function DisplayHelpFile()
        {
          var i =  PageMethods.DisplayHelpPage();// I think this is wrong
          alert(i);
        }   

Web方法
[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]   
     [System.Web.Script.Services.ScriptService]
    public class ShowHelpPage : System.Web.Services.WebService
    {
        [WebMethod]
        public string DisplayHelpPage()
        {
            return "Window.Html";            
        }
    }

如何从母版中调用Webservice函数

"我在网上看到,如果我想在后面的代码中调用函数然后我必须创建一个web服务并调用webservice方法从主页

我假设在上下文中你指的是方法而不是函数。在加载母版页时是否要调用一个方法?我认为你不需要为此创建一个Web服务?除非您希望客户端通过Async发起请求,并且您正在使用JavaScript和AJAX发出请求。你想达到什么目标?在我看来,这可以通过javascript实现。如果你通过JavaScript调用一个页面,它应该自己加载吗?

下面实现你想要的,但只使用javascript。

     function keyHandler(e)
     {
         ... key handling script
         if(myKey == 'F1')
         {   
             window.location.href = '...';              
         }
     }
     document.onkeypress = keyHandler;

如果你需要调用这个Web服务,它需要从客户端启动,你可以使用JQuery来做到这一点。对showhelpage使用相同的方法。asmx存储处理和通过JavaScript处理的呈现。据我所知,这也消除了对ToolkitScriptManager的依赖。

function DisplayHelpFile()         
{   
     $.ajax({
              type: "POST",
              url: "ShowHelpPage.asmx/DisplayHelpPage",
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success:function(result)
              {
                  alert(result);
              }
         },
         error: ErrorMessage
      });
      function ErrorMessage(result)
      {
        alert(result.status + ' ' + result.statusText + ' ' + result.responseText);
      }    
}    

你的web方法需要是静态的