Skip to content Skip to sidebar Skip to footer

How To Call Code Behind Method From A Javascript Function?

I am having an javascript function for a HTML img click event in aspx page. And a server Method in its code behind page.Now I want to call the server method from the javascript fun

Solution 1:

.aspx

<div><p>Say bye-bey to Postbacks.</p><asp:ScriptManagerID="ScriptManager1"runat="server"EnablePageMethods="true"></asp:ScriptManager><asp:TextBoxID="txtname"runat="server"></asp:TextBox><br /><asp:TextBoxID="txtaddress"runat="server"></asp:TextBox><br /><asp:ButtonID="btnCreateAccount"runat="server"Text="Signup"OnClientClick="HandleIT(); return false;" /></div>

JavaScript

<scripttype="text/javascript">functionHandleIT() {
            var name = document.getElementById('<%=txtname.ClientID %>').value;
            var address = document.getElementById('<%=txtaddress.ClientID %>').value;
            PageMethods.ProcessIT(name, address, onSucess, onError); 
            functiononSucess(result) {
                alert(result);
            }
            functiononError(result) {
                alert('Something wrong.');
            }
        }
    </script>

C#

 [WebMethod]
    publicstaticstringProcessIT(string name, string address)
    {
        string result = "Welcome Mr. " + name + ". Your address is '" + address + "'.";
        return result;
    }

Solution 2:

The WebMethods that are defined must be "static".

The following ajax call to a WebMethod "GetAllRegions" works fine, provided, the WebMethod is defined as static!!

$.ajax({
  type: "POST",
  url: 'GenerateEAInHtml.aspx/GetAllRegions',
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
});

The WebMethod is defined this way:

[WebMethod]
publicstatic List<Region> GetAllRegions()
{
  /* Your Code goes here */return regions;
}

You might want to use this Ajax Post to also pass JSON data to the WebMethod by passing the data parameter!! This should help.

Solution 3:

Assumption that your page name is abc.aspx and xyz is the function name that you have to call. javascript :

function sendData(offset) {
    $.ajax({
    type: "POST",
    url: "abc.aspx/xyz",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
    });
}

Server Side : add Imports System.Web.Script.Serialization

WebMethod(enableSession:=True) _
Public Shared Function xyz() As String
    your code here
End Function

Add module in web.config -> system.webServer

->system.webServer>
    ->modules>
    ->add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    ->/modules>
->/system.webServer>

Solution 4:

Back end C# code is separate from your front end JavaScript code. JS runs on the client side and C# runs server side. In order to have front end code fire off a back end functions, you would either need your function to do a form postback or use Ajax (or an equivalent) to call that pages function.

Solution 5:

Try this

[WebMethod]

  publicstaticvoidPopUpClick(object sender, EventArgs e)
    {
        //Something;
    }

Post a Comment for "How To Call Code Behind Method From A Javascript Function?"