Call ASP.NET codebehind from javascript (PageMethod)

The first thing to do is to add a ScriptManager to our page,
and tell it to accept page Methods.
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="True"></asp:ScriptManager>

Next add the codebehind method to call, it must be static and have
the WebMethod attribute

[WebMethod()]
public static void SetDimensions(int width, int height)
{        
    //Method code
}

Finally call the method using PageMethods 

<script type="text/javascript">
    function setDimensions() {
        PageMethods.SetDimensions(width, height);
    }
</script>
Advertisements

%d bloggers like this: