Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

javascript - 从JavaScript调用ASP.NET函数?(Call ASP.NET function from JavaScript?)

I'm writing a web page in ASP.NET.(我正在用ASP.NET编写一个网页。)

I have some JavaScript code, and I have a submit button with a click event.(我有一些JavaScript代码,我有一个带有click事件的提交按钮。) Is it possible to call a method I created in ASP with JavaScript's click event?(是否可以使用JavaScript的click事件调用我在ASP中创建的方法?)   ask by MattSayar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.(II。) This should add (using Tab - Tab ) this function to your code file:(这应该添加(使用Tab - Tab )此函数到您的代码文件:) public void RaisePostBackEvent(string eventArgument) { } iii.(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.(这将在您的代码文件中调用'RaisePostBackEvent'方法,并将'eventArgument'作为您从JavaScript传递的'argumentString'。) Now, you can call any other event you like.(现在,您可以调用您喜欢的任何其他活动。) PS: 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不允许我写入下划线后跟一个角色!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...