ASP.NET 4.0 - 从客户端方法调用服务器端方法 - PageMethods

发布于 2024-09-11 15:12:52 字数 1685 浏览 3 评论 0原文

我正在尝试从客户端访问服务器端方法(即从 javascript 调用服务器方法)。我正在使用 .NET 4.0 Visual Studio 2010 Ultimate。我正在构建一个网络控件(ascx)。 我有一个 ascx 页面(控件的 html)和一个 ascx.cs 页面: ascx 如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BoxButton.ascx.cs" Inherits="ATB.Controls._BoxButton" %>

<asp:ScriptManager ID="SM_ScriptManager" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
    function handle() {
        PageMethods.manageHandler();
    }
</script>
<span id="BoxButton_HtmlSpan" runat="server" onclick="handle();" style="background-color:#efefef;border-style:solid;border-width:1px;border-color:#cccccc;height:25px;text-align:center;padding:3px 3px 3px 3px;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;color:#333333;line-height:15px">
    <asp:Label runat="server" Text="Button" id="Text_Label"></asp:Label>
</span>

ascx.cs 文件就是这个(仅打印函数):

[System.Web.Services.WebMethod]
   public static string manageHandler() {
   int i = 0;
   System.Console.WriteLine("Ciao");
   return "Hello";
}

好吧,正如你所看到的,我正在尝试通过 pagemethods 解决我的问题,但它不起作用,我总是 getPageMethods 未定义由 JScript 运行时管理器。 那么,PageMethods 是否有可能在 .net 4.0 中不可用?

无论如何,是否有另一种方法可以解决有关从客户端脚本调用服务器端方法的问题?我知道 pagemethods 只不过是一个 Web 服务,当考虑我的 Web 应用程序的安全性时,这让我有点困扰。

编辑:

嗯,看起来 jQuery 是一个有效的解决方案...但是它能够允许我调用服务器端方法吗?我希望是因为我们正在谈论 ajax,对吧?

编辑2:

啊...另一个问题...我尝试使用webmethod,但它不起作用,当我按指定设置scriptmanager和方法的属性WebMethod时,它不会在javascript中找到PageMethods错误。 ..

是因为我在 ascx 中工作吗??????可能的?????那么,即使 jQuery 也无济于事......

I'm trying to access a server side method from a client side one (that is just calling a server method from javascript). I'm using .NET 4.0 Visual Studio 2010 Ultimate. I'm building a web control (ascx).
I have an ascx page (the control's html) and an ascx.cs page:
The ascx is the following:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BoxButton.ascx.cs" Inherits="ATB.Controls._BoxButton" %>

<asp:ScriptManager ID="SM_ScriptManager" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
    function handle() {
        PageMethods.manageHandler();
    }
</script>
<span id="BoxButton_HtmlSpan" runat="server" onclick="handle();" style="background-color:#efefef;border-style:solid;border-width:1px;border-color:#cccccc;height:25px;text-align:center;padding:3px 3px 3px 3px;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;color:#333333;line-height:15px">
    <asp:Label runat="server" Text="Button" id="Text_Label"></asp:Label>
</span>

And the ascx.cs file is this one (just printing the function):

[System.Web.Services.WebMethod]
   public static string manageHandler() {
   int i = 0;
   System.Console.WriteLine("Ciao");
   return "Hello";
}

Well, as you can see I'm trying to solve my problem through pagemethods, well it does not work and I always getPageMethods is undefined b y the JScript runtime manager.
Well, is it possible that PageMethods are not available in .net 4.0?

Anyway, is there another approach for solving the problem regarding server side method calling from client side scripts? I understood that pagemethods are nothing more than a web service, and this bothers me a little when thinking about security in my web app.

EDIT:

Well, it seems that jQuery is a valid solution... but is it able to allow me to call a server side method? I expect to be because we're talking about ajax right?

EDIT 2:

Ah... Another question... I tried to use a webmethod but it does not work getting me errors not finding PageMethods in javascript while I set scriptmanager and method's attribute WebMethod as specified....

Is it because I'm working inside an ascx?????? possible????? Then, even jQuery will not help....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2024-09-18 15:12:52

我相信 PageMethods 只能在页面上使用,而不能在用户控件上使用。

I believe that PageMethods are only available on pages, not on user controls.

泡沫很甜 2024-09-18 15:12:52

我建议使用 jQuery,它更加优雅。 这是一个示例

I would recommend using jQuery, it's much more elegant. Here's an example

云醉月微眠 2024-09-18 15:12:52

我找到了另一个使用 JQuery 执行此操作的教程: 从 JQUERY 调用服务器端函数< /a>

希望这会有所帮助。

I have found one more tutorial to do it with JQuery: Calling a server side function from JQUERY

hope this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文