Home > DeveloperSection > Forums > Call ASP.NET Function From Javascript?
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    February-13-2013 1:54 AM

 C# C# 

 1 Reply(s)
 1389  View(s)
Rate this:

Hi Everyone!

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?

Please reply SASP

Thanks in advance




Total Post:604

Posted on    February-13-2013 8:06 AM

Hi Ankita!

There are three way to call asp.net function from JavaScript

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.

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!

Don't want to miss updates? Please click the below button!

Follow MindStick