asp.net的ScriptManager PageMethods未定义 [英] asp.net ScriptManager PageMethods is undefined

查看:405
本文介绍了asp.net的ScriptManager PageMethods未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话从JS静态的服务器端方法,所以我决定使用ScriptManager控件在我的网站。 所以我有一个母版页,这样的结构:

I want to call static server-side methods from JS so i decide to use ScriptManager control on my site. So i have a master page, with such structure:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="TopLevelMasterPage.Master.cs"
    Inherits="Likedrive.MasterPages.TopLevelMasterPage" %>

<!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"
      xmlns:fb="http://ogp.me/ns/fb#">

<head runat="server">
    <title></title>
        <script type="text/javascript">
            function getGiftFileUrl() {
                function OnSuccess(response) {
                    alert(response);
                }
                function OnError(error) {
                    alert(error);
                }

                PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSuccess, OnError);
            }

            getGiftFileUrl();

        </script>
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManagerMain"
            runat="server"
            EnablePageMethods="true" 
            ScriptMode="Release" 
            LoadScriptsBeforeUI="true">
    </asp:ScriptManager>
    <asp:ContentPlaceHolder ID="MainContent" runat="server"> 
    </asp:ContentPlaceHolder>
    </form>
</body>
</html>

但是,当加载页面时,我有一个JS例外 - PageMethods是不确定的。 我推测,对象将被创建隐含的,所以我可以在我的JavaScript中使用它。

But when page is loading, i have a JS exception - PageMethods is undefined. I supposed that object will be created implicit so i can use it in my javascript.

推荐答案

谢谢你们的答复!我明白了为什么PageMethod的对象undefinded,因为ScriptManager的组件从使用PageMethod的脚本放在旁边,所以当页面渲染和脚本的执行,也没有PageMethod的在这一刻。所以,我需要调用getGiftFileUrl()上按一下按钮或在窗口load事件,当页面上的所有脚本都可以使用了。

Thanks guys for responses! I've realize why the PageMethod object was undefinded, because ScriptManager component placed next from the script that uses PageMethod, so when page is rendered and script executed, there is no PageMethod at this moment. So i need to call getGiftFileUrl() on button click or on window load event, when all scripts on page are ready to use.

这篇关于asp.net的ScriptManager PageMethods未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆