且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 JavaScript 调用 ASP.NET 函数?

更新时间:2023-02-15 21:10:31

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

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):

虽然有点棘手... :)

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

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

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

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

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

public void RaisePostBackEvent(string eventArgument) { }

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

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

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

这将调用代码文件中的RaisePostBackEvent"方法,并将eventArgument"作为您从 JavaScript 传递的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.

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

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!