Link to home
Start Free TrialLog in
Avatar of rajeeva_nagaraj
rajeeva_nagaraj

asked on

Call Code behind method from javascript

Hi,

I have written a java script function which shows an alert message after some calculation, now i want to execute a code behind method (C# method) after that alert is shown.
Please its very urgent.

Regards
Rajeeva
ASKER CERTIFIED SOLUTION
Avatar of abhi376
abhi376
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ph_o_enix
ph_o_enix

Use this

function CallBackRequest(url,params,Callback)
{
    var xmlHttp;
    try
    {  
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try
        {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
          alert("Your browser does not support AJAX!");
          return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
    {
      if(xmlHttp.readyState==4)
        Callback(xmlHttp.responseText);
    }
    if(params== null)
    {
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
    }
    else
    {
      xmlHttp.open("POST",url,true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", params.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(params);
    }
}
you may use _doPostBack method to execute server side event..like

__doPostBack('ButtonID','')
// if you are using master page than use <%= ButtonID.ClientID %>

for more info

http://wiki.asp.net/page.aspx/1082/dopostback-function/
Avatar of Amit Tripathi
You can use XML http for it ,it's goes in to server and do what ever you want....
Avatar of rajeeva_nagaraj

ASKER

Hi All

I found solution, code behind method can be called from java script but using web methods