从 JavaScript 调用 ASP.NET 函数? [英] Call ASP.NET function from JavaScript?

查看:34
本文介绍了从 JavaScript 调用 ASP.NET 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

好吧,如果您不想使用 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!

这篇关于从 JavaScript 调用 ASP.NET 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆