vote up 9 vote down star
6

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?

flag

8 Answers

vote up 8 vote down check

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!

link|flag
vote up 8 vote down

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.

link|flag
vote up 4 vote down

You can do it Asynchronously using .NET Ajax PageMethods.

See here or here.

link|flag
vote up 2 vote down

You should be using some Ajax library like : Anthem

link|flag
vote up 2 vote down

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();
link|flag
This worked great for me! – mattdell Dec 4 at 21:00
vote up 1 vote down

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.

link|flag
vote up 1 vote down

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.

link|flag
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 Jul 16 at 16:37
vote up 0 vote down

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

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.