igo*_*GIS 11 javascript asp.net ajax scriptmanager
我想从JS调用静态服务器端方法,所以我决定在我的网站上使用ScriptManager控件.所以我有一个母版页,具有这样的结构:
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
但是当页面加载时,我有一个JS异常 - 未定义PageMethods.我认为该对象将被隐式创建,所以我可以在我的javascript中使用它.
Vit*_*ova 24
要使用PageMethods,您需要按照以下步骤操作:
ScriptManager和设置EnablePageMethods.(你做过).static在后面的代码中创建一个方法并使用该[WebMethod]属性.sucess和error.(你做过).你错过了这些步骤吗?
编辑:刚刚意识到你这样做了:
function getGiftFileUrl() {
function OnSuccess...
Run Code Online (Sandbox Code Playgroud)
你有一个功能内的回调.你需要你的回调像这样:
function OnSuccess(response) {
alert(response);
}
function OnError(error) {
alert(error);
}
PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSuccess, OnError);
Run Code Online (Sandbox Code Playgroud)
而你后面的代码可能会以类似的方式结束:
[WebMethod]
public static string GetGiftFileUrl(string name, int width, int height)
{
//... work
return "the url you expected";
}
Run Code Online (Sandbox Code Playgroud)
额外奖励:因为这是一种static你无法使用的方法this.Session["mySessionKey"],但你可以做到HttpContext.Current.Session["mySessionKey"].
我已经意识到为什么未定义 PageMethod 对象,因为 ScriptManager 组件放置在使用 PageMethod 的脚本的下一个位置,因此当渲染页面并执行脚本时,此时没有 PageMethod。因此,当页面上的所有脚本都可以使用时,我需要在按钮单击或窗口加载事件上调用 getGiftFileUrl() 。
| 归档时间: |
|
| 查看次数: |
60934 次 |
| 最近记录: |