views:

28964

answers:

10

I'm writing a web page in ASP.NET. I have some Javascript, and I have a submit button with an onClick event.

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

+2  A: 

You should be using some Ajax library like : Anthem

jdecuyper
+4  A: 

You can do it Asynchronously using .NET Ajax PageMethods.

See here or here.

brendan
+8  A: 

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.

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

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:

var myVar = 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.

EndangeredMassa
+11  A: 

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

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

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

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

public void RaisePostBackEvent(string eventArgument) { }

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

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.

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!

Adhip Gupta
+1  A: 

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.

Lars Mæhlum
+4  A: 

The __doPostBack() method works well.

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

<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.

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

button.click();
GoodEnough
This worked great for me!
mattdell
what if i want to use an argument ? can i get it from client side and use it with button.Click() ?
Kubi
@Kubi an argument on the server side? What I usually do is serialize the arguments that need to be sent to the server side and put them in a hidden field.
GoodEnough
could you please look at here ? http://stackoverflow.com/questions/2536106/calling-a-non-static-method-in-server-side-by-a-client-side-function
Kubi
does the Click method exist??
Saher
@saher yes, but it's actually .click (lowercase c) (http://www.comptechdoc.org/independent/web/cgi/javamanual/javabutton.html)
GoodEnough
+1  A: 

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.

This is the library called AjaxPro which was written a MVP named Michael Schwarz. This was library was not written by 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 will with MS version of Ajax with no issues. However I would note with how easy MS has made Ajax I would only use it if really necessary. It takes allot of JavaScript to do some really complicated functionality that you get from MS by just dropping it into an update panel.

David Basarab
I wish this post had been closer to the one above... I blew several minutes trying to figure out why that method was mentioned nowhere in the MS documentation :/
Dave Swersky
@Dave That's why people should comment on answers and not add comments to other answers as unique answers themselves.
Charles Boyung
A: 

Making a javascript function that calls an ASP.NET server side webservice is probably the easiest way.

A: 

this reply works like a breeze for me thanks cross browser

The __doPostBack() method works well.

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

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

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

button.Click();

Blockquote

Mr.X
A: 

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

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