使用 jquery 调用 ascx 页面方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/579024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:22:59  来源:igfitidea点击:

calling an ascx page method using jquery

asp.netjqueryweb-servicesasmxpagemethods

提问by codette

I know that I can call a page method with jquery using the following syntax

我知道我可以使用以下语法使用 jquery 调用页面方法

$.ajax({
  type: "POST",
  url: "Default.aspx/GetDate",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Replace the div's content with the page method's return.
    $("#Result").text(msg.d);
  }
});

This works for aspxpages but is it possible with ascxpages? (web controls)

这适用于aspx页面,但是ascx页面可以吗?(网页控件)

I've been trying it for about half an hour and since I can't get it to work I'm wondering if it's even possible.

我已经尝试了大约半个小时,因为我无法让它工作,所以我想知道它是否可能。

Note: Just to be clear, when I try to call the ascx page I amupdating the url in jquery :)

注意:为了清楚起见,当我尝试调用 ascx 页面时,我正在更新 jquery 中的 url :)

回答by Clyde

No, because ascx controls don't represent a real URL that can be accessed from a client machine. They're purely server-side meant to embed in other pages.

不,因为 ascx 控件不代表可以从客户端计算机访问的真实 URL。它们纯粹是服务器端,旨在嵌入其他页面。

What you might want to do is just have an aspx page that provides the same snippet of html you currently have in your ascx file. An aspx page doesn't necessarily need to provide a full html document (<html><body> etc.), it can just render the user control you're interested in.

您可能想要做的只是有一个 asx 页面,该页面提供与您当前在 ascx 文件中相同的 html 片段。aspx 页面不一定需要提供完整的 html 文档(<html><body> 等),它可以只呈现您感兴趣的用户控件。

We use this technique all the time with the ingrid plugin, which requires a callback url for the table contents.

我们一直在 ingrid 插件中使用这种技术,它需要表内容的回调 url。

回答by Rex Miller

I don't think it's possible by requesting the ascx file directly--i.e. supplying "MyControl.ascx" as the url parameter to $.ajax(..). The ascx file isn't exposed directly by the web server.

我认为直接请求 ascx 文件是不可能的——即提供“MyControl.ascx”作为 $.ajax(..) 的 url 参数。Web 服务器不会直接公开 ascx 文件。

You can, I believe, supply the url of the aspx page containing the user control--i.e. if an instance of MyControl.ascx lives on MyPage.aspx you would have to supply "MyPage.aspx" as the url parameter. It sounds like that might defeat the purpose for what you're trying to accomplish though.

我相信,您可以提供包含用户控件的 aspx 页面的 url——即,如果 MyControl.ascx 的实例存在于 MyPage.aspx 上,则必须提供“MyPage.aspx”作为 url 参数。听起来这可能会破坏您尝试完成的目的。

EDIT: What Clyde said below seems like a good idea. I'm doing something similar myself by including the ascx control on a page whose job is more or less just to host it for access from client-side script.

编辑:克莱德在下面所说的似乎是个好主意。我自己也在做类似的事情,将 ascx 控件包含在页面上,该页面的工作或多或少只是为了托管它以便从客户端脚本访问。

回答by Chad Scharf

Here is a way to get around the limitation of only having Page methods (ScriptMethod) available in ASPX pages. This example builds a proxy using a page base class and a special attribute to decorate methods in your ASCX page's code-behind and have those accessible via the client.

这是一种绕过 ASPX 页面中只有页面方法 (ScriptMethod) 可用的限制的方法。此示例使用页面基类和特殊属性构建代理,以装饰 ASCX 页面代码隐藏中的方法,并让这些方法可通过客户端访问。

Creating a Page method (ScriptMethod) within an ASCX user control using AJAX, JSON, base classes and reflection

使用 AJAX、JSON、基类和反射在 ASCX 用户控件中创建页面方法 (ScriptMethod)

回答by eglasius

I don't think it will be possible, as user controls aren't meant to be accessible externally (outside of a page). I suggest just using a script service (a web service).

我认为这是不可能的,因为用户控件并不意味着可以从外部(页面之外)访问。我建议只使用脚本服务(Web 服务)。

回答by RSolberg

What about creating the method on the .aspx page that does what it needs to with the information from the control? I know people go back and fourth on what controls are supposed to contain or not contain, but if the control only contains properties and objects, I would think having the function in the .aspx page could work for you. Obviously there would be many trade-offs.

如何在 .aspx 页面上创建使用控件信息执行所需操作的方法?我知道人们会反复讨论哪些控件应该包含或不包含,但如果控件只包含属性和对象,我认为在 .aspx 页面中拥有该功能可能对您有用。显然会有很多取舍。

回答by hunter

I would create a Generic Handler (.ashx) which Loads the control and writes its rendered HTML to the Response.

我将创建一个通用处理程序 (.ashx),它加载控件并将其呈现的 HTML 写入响应。

回答by Ch?a bi?t

You can write method GetDate() in Default.aspx or an other aspx file AND YOU CAN CALL THAT METHOD FROM .ASCX FILE.

您可以在 Default.aspx 或其他 aspx 文件中编写方法 GetDate() 并且您可以从 .ASCX 文件中调用该方法

Ex:

前任:

In UserControl.ascx:

在 UserControl.ascx 中:

$.ajax({
 type: "POST",
 url: "Default.aspx/GetDate",
 data: "{}",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function(msg) {
 // Replace the div's content with the page method's return.
 $("#Result").text(msg.d);
}

});

});

In Default.aspx.cs:

在 Default.aspx.cs 中:

Public void GetDate() //Public static void { //your code here }

Public void GetDate() //Public static void { //your code here }