ASP.NET调用函数从Javascript? [英] Call ASP.NET Function From Javascript?

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

问题描述

我正在写在ASP.NET网页。我有一些JavaScript和我有一个click事件的提交按钮。

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

是否有可能叫我在ASP中创建了JavaScript的点击事件的方法?

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

这是一个有点棘手,虽然...:)

我。在您的code文件(假设你使用C#和.NET 2.0或更高版本)中添加以下界面到您的网页类,使它看起来像

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),此函数到code文件:

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

public void RaisePostBackEvent(string eventArgument) { }

三。在你的Javascript onclick事件写入以下code:

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

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

这将调用RaisePostBackEvent'的方法在code文件与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:那就是'下划线下划线doPostBack......而且,应该按照这个顺序没有空间......不知不觉中,这些大规模杀伤性武器并没有让我写为下划线后跟一个字符

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!

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

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