How to call a codebehind function from javascript in asp.net?

You can do this by an ajax call

this is a jquery example:

$.ajax({
            type: "POST",
     url:"~/code_behind.aspx/Method",
            data: dataPost,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
    ....
    });

here is api documentation and in code behind

[WebMethod]
public static yourType Method (Params){}

or you can add a hidden button inside updatePanel, and invoke the click event using js. ('#<%=ID.ClientID%>').click(); It will invoke the OnClientClick if it exists then your codeBehind fucntion.


in JavaScript:

    document.getElementById("btnSample").click();

Server side control:

    <asp:Button runat="server" ID="btnSample" ClientIDMode="Static" Text="" style="display:none;" OnClick="btnSample_Click" />

C#

    protected void btnSample_Click(object sender, EventArgs e)
    {

    }

It is easy way though...