C# 从 JavaScript 调用 ASP.NET 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3713/
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-07-31 13:38:33  来源:igfitidea点击:

Call ASP.NET function from JavaScript?

提问by MattSayar

I'm writing a web page in ASP.NET. I have some JavaScript code, and I have a submit button with a click event.

我正在用 ASP.NET 编写一个网页。我有一些 JavaScript 代码,我有一个带有点击事件的提交按钮。

Is it possible to call a method I created in ASP with JavaScript's click event?

是否可以使用 JavaScript 的单击事件调用我在 ASP 中创建的方法?

采纳答案by Adhip Gupta

Well, if you don't want to do it using Ajax or any other way and just want a normal ASP.NET postback to happen, here is how you do it (without using any other libraries):

好吧,如果您不想使用 Ajax 或任何其他方式来执行此操作,而只想进行正常的 ASP.NET 回发,那么您可以这样做(不使用任何其他库):

It is a little tricky though... :)

虽然有点棘手... :)

i. In your code file (assuming you are using C# and .NET 2.0 or later) add the following Interface to your Page class to make it look like

一世。在您的代码文件中(假设您使用的是 C# 和 .NET 2.0 或更高版本)将以下接口添加到您的 Page 类中,使其看起来像

public partial class Default : System.Web.UI.Page, IPostBackEventHandler{}

ii. This should add (using Tab-Tab) this function to your code file:

ii. 这应该添加(使用Tab- Tab)这个函数到你的代码文件中:

public void RaisePostBackEvent(string eventArgument) { }

iii. In your onclick event in JavaScript, write the following code:

三、在 JavaScript 中的 onclick 事件中,编写以下代码:

var pageId = '<%=  Page.ClientID %>';
__doPostBack(pageId, argumentString);

This will call the 'RaisePostBackEvent' method in your code file with the 'eventArgument' as the 'argumentString' you passed from the JavaScript. Now, you can call any other event you like.

这将调用代码文件中的“RaisePostBackEvent”方法,并将“eventArgument”作为从 JavaScript 传递的“argumentString”。现在,您可以调用您喜欢的任何其他事件。

P.S: That is 'underscore-underscore-doPostBack' ... And, there should be no space in that sequence... Somehow the WMD does not allow me to write to underscores followed by a character!

PS:那是“下划线-下划线-doPostBack”......而且,该序列中不应该有空格......不知何故WMD不允许我写下划线后跟一个字符!

回答by brendan

You can do it asynchronously using .NET Ajax PageMethods. See hereor here.

您可以使用 .NET Ajax PageMethods 异步执行此操作。请参阅此处此处

回答by EndangeredMassa

The Microsoft AJAX librarywill accomplish this. You could also create your own solution that involves using AJAX to call your own aspx (as basically) script files to run .NET functions.

微软AJAX库将做到这一点。您还可以创建自己的解决方案,该解决方案涉及使用 AJAX 调用您自己的 aspx(基本上)脚本文件来运行 .NET 函数。

I suggest the Microsoft AJAX library. Once installed and referenced, you just add a line in your page load or init:

我建议使用 Microsoft AJAX 库。安装并引用后,您只需在页面加载或初始化中添加一行:

Ajax.Utility.RegisterTypeForAjax(GetType(YOURPAGECLASSNAME))

Then you can do things like:

然后你可以做这样的事情:

<Ajax.AjaxMethod()> _
Public Function Get5() AS Integer
    Return 5
End Function

Then, you can call it on your page as:

然后,您可以在您的页面上调用它:

PageClassName.Get5(javascriptCallbackFunction);

The last parameter of your function call must be the javascript callback function that will be executed when the AJAX request is returned.

函数调用的最后一个参数必须是返回 AJAX 请求时将执行的 javascript 回调函数。

回答by Lars M?hlum

You might want to create a web service for your common methods.
Just add a WebMethodAttribute over the functions you want to call, and that's about it.
Having a web service with all your common stuff also makes the system easier to maintain.

您可能希望为常用方法创建 Web 服务。
只需在您要调用的函数上添加一个 WebMethodAttribute,就可以了。
拥有包含所有常用内容的 Web 服务还可以使系统更易于维护。

回答by mbillard

The __doPostBack()method works well.

__doPostBack()方法效果很好。

Another solution (very hackish) is to simply add an invisible ASP button in your markup and click it with a JavaScript method.

另一种解决方案(非常骇人听闻)是简单地在标记中添加一个不可见的 ASP 按钮,然后使用 JavaScript 方法单击它。

<div style="display: none;">
   <asp:Button runat="server" ... OnClick="ButtonClickHandlerMethod" />
</div>

From your JavaScript, retrieve the reference to the button using its ClientIDand then call the .click()method on it.

从您的 JavaScript 中,使用其ClientID检索对按钮的引用,然后对其调用.click()方法。

var button = document.getElementById(/* button client id */);

button.click();

回答by David Basarab

The Microsoft AJAX library will accomplish this. You could also create your own solution that involves using AJAX to call your own aspx (as basically) script files to run .NET functions.

Microsoft AJAX 库将完成此操作。您还可以创建自己的解决方案,该解决方案涉及使用 AJAX 调用您自己的 aspx(基本上)脚本文件来运行 .NET 函数。

This is the library called AjaxPro which was written an MVP named Michael Schwarz. This was library was not written by Microsoft.

这是一个名为 AjaxPro 的库,它是由一位名为Michael Schwarz的 MVP 编写的。这是库不是由 Microsoft 编写的。

I have used AjaxPro extensively, and it is a very nice library, that I would recommend for simple callbacks to the server. It does function well with the Microsoft version of Ajax with no issues. However, I would note, with how easy Microsoft has made Ajax, I would only use it if really necessary. It takes a lot of JavaScript to do some really complicated functionality that you get from Microsoft by just dropping it into an update panel.

我广泛地使用了 AjaxPro,它是一个非常好的库,我会推荐它用于对服务器的简单回调。它在 Microsoft 版本的 Ajax 中运行良好,没有任何问题。但是,我要指出,由于 Microsoft 使 Ajax 变得如此简单,我只会在真正必要时使用它。只需将其放入更新面板中,就需要大量 JavaScript 才能完成一些从 Microsoft 获得的非常复杂的功能。

回答by Ananda

This reply works like a breeze for me thanks cross browser:

感谢跨浏览器,这个回复对我来说轻而易举:

The __doPostBack() method works well.

__doPostBack() 方法运行良好。

Another solution (very hackish) is to simply add an invisible ASP button in your markup and click it with a JavaScript method.

另一种解决方案(非常骇人听闻)是简单地在标记中添加一个不可见的 ASP 按钮,然后使用 JavaScript 方法单击它。

<div style="display: none;"> 
    <asp:Button runat="server" ... OnClick="ButtonClickHandlerMethod" /> 
</div> 

From your JavaScript, retrieve the reference to the button using its ClientID and then call the .Click() method on it:

从您的 JavaScript 中,使用其 ClientID 检索对按钮的引用,然后对其调用 .Click() 方法:

var button = document.getElementByID(/* button client id */); 

button.Click(); 

Blockquote

块引用

回答by Riga

If the __doPostBack function is not generated on the page you need to insert a control to force it like this:

如果页面上没有生成 __doPostBack 函数,您需要插入一个控件来强制它,如下所示:

<asp:Button ID="btnJavascript" runat="server" UseSubmitBehavior="false" />

回答by Shuja

Add this line to page load if you are getting object expected error.

如果您收到对象预期错误,请将此行添加到页面加载中。

ClientScript.GetPostBackEventReference(this, "");

回答by kakani santosh

You can use PageMethods.Your C# method Namein order to access C# methods or VB.NET methods into JavaScript.

您可以使用PageMethods.Your C# method Name以便将 C# 方法或 VB.NET 方法访问到 JavaScript。