PageMethods未定义[尽我所能尝试] [英] PageMethods is not defined [Tried everything i could]
本文介绍了PageMethods未定义[尽我所能尝试]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尽我所能,但仍然没有运气。我的错在哪里?
Default2.aspx
i tried everything i could, but still no luck. where is my mistake?
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function InsertLabelData() {
PageMethods.GetLabelText(onSuccess, onFailure);
}
function onSuccess(result) {
var lbl = document.getElementById('lbl');
lbl.innerHTML = result;
}
function onFailure(error) {
alert(error);
}
InsertLabelData();
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label id="lbl"></label>
<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
</asp:ScriptManager>
</div>
</form>
</body>
</html>
Default2.aspx.cs
Default2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetLabelText()
{
return "Hello";
}
}
推荐答案
Hii,
好PageMethods一直是一个有争议的话题,但我相信当你使用控件时你会被调用它们。在这种情况下,如果您放置一个Javascript事件,例如''onmouseover'',那么它可以完美地工作......
Hii,
Well PageMethods have always been a controversial topic but I believe they will be called when you have an event attached to the control whihc you are using. In this case if you put a Javascript event such as ''onmouseover'', then it works perfectly...
ex:
<script type="text/javascript">
function GetLabel() {
PageMethods.GetLabelText(onsuccess, onfailure);
}
function onsuccess(result) {
var x = document.getElementById('lblnew');
x.innerHTML = result;
}
function onfailure(error) {
alert(error);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label id="lblnew" onmouseover="GetLabel();"> hiii</label>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
</div>
</form>
</body>
</html>
aspx.cs:
aspx.cs:
[System.Web.Services.WebMethod]
public static string GetLabelText()
{
return "Dark Knight";
}
调用普通的Javascript函数不能正常工作,因为标签控件不知道何时调用它。尝试这样,你会理解很多东西。
快乐编码......
Calling a plain Javascript function won''t work IMO because the label control doesnot know when to call it. Try like this and you will understand much stuffs.
Happy Coding...
这篇关于PageMethods未定义[尽我所能尝试]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文