无法从OnPaste事件调用Page方法
本文关键字:调用 Page 方法 事件 OnPaste | 更新日期: 2023-09-27 18:24:57
在IE中,当我从OnPaste事件调用页面方法时,当我点击输入框并点击CTRL+V:时,我会得到以下错误
0x80070057 - JavaScript runtime error: Invalid argument.
在这一行代码:
this._xmlHttpRequest.open(verb, this._webRequest.getResolvedUrl(), true );
调用堆栈如下所示:
Sys$Net$XMLHttpExecutor$executeRequest [ScriptResource.axd] Line 6055 Script
Sys$Net$_WebRequestManager$executeRequest [ScriptResource.axd] Line 6302 Script
Sys$Net$WebRequest$invoke [ScriptResource.axd] Line 6481 Script
Sys$Net$WebServiceProxy$invoke [ScriptResource.axd] Line 6923 Script
Sys$Net$WebServiceProxy$_invoke [ScriptResource.axd] Line 6807 Script
PageMethods.prototype.Message [WebForm1.aspx] Line 76 Script
PageMethods.Message [WebForm1.aspx] Line 117 Script
GetMessage [WebForm1.aspx] Line 11 Script
onpaste [WebForm1.aspx] Line 125 Script
我在一个小项目中复制了它。以下是WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PageMethodTest.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript'>
function GetMessage() {
PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}
function OnGetMessageSuccess(result, userContext, methodName) {
alert(result);
}
function OnGetMessageFailure(error, userContext, methodName) {
alert(error.get_message());
}
</script>
</head>
<body>
<form id='form1' runat='server'>
<input value="paste here" onpaste="GetMessage()" onblur="GetMessage()" />
<asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
<div>
<input type='submit' value='Get Message' onclick='GetMessage(); return false;' />
</div>
</form>
</body>
</html>
这是WebForm1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PageMethodTest
{
public partial class WebForm1 : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static string Message()
{
return "Hello from the server-side World!";
}
}
}
我从OnBlur事件中调用了相同的页面方法,当你从输入框中弹出时,它工作得很好。这个调用PageMethod的OnPaste事件在Chrome中运行良好。IE的OnPaste事件与大多数其他事件的上下文有什么不同吗?
谢谢。
不确定到底发生了什么,但症状与本问题中描述的问题相同。IE与onpaste
事件相结合,再加上发出AJAX请求,出现了一些问题。
我能够通过使用具有小延迟的setTimeout
来解决这个问题:
function GetMessage() {
setTimeout(function () {
PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}, 100);
}