CALL ASP.NET FUNCTION FROM JAVASCRIPT?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 1493  View(s)
Ratings:
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

 


 

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Call ASP.NET Function From Javascript?

    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!

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!