0

Javascript code

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server" >
 <script type="text/javascript">
function ValidatePage() 
{
  PageMethods.SaveAndMoveTo_Page3("abcd", CallSuccess, CallFailure);
}
</script
</asp:content>

Button event will trigger a call to the function validatePage()

<asp:Button ID="Button2" runat="server" style="background-color: #CC0D0D; border-radius: 5px; " 
    ForeColor="#c8c8c8" OnClientClick ="ValidatePage();" Text="Next" Width="120px" />

aspx page <asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' /> is already added in the site master.

I need the Javascript method to call SaveAndMoveTo_Page3() method in the code. Somehow PageMethods is not calling the method in my C# code.

Please help.

  • You will need AJAX. EDIT: I think you are not understanding the concept of server <--> client. all your asp.net code runs on the server and javascript runs on the client in the browser – RononDex Jun 25 '15 at 08:58
  • AJAC call will help you – AkshayJ Jun 25 '15 at 08:58
  • Try creating a javascript callback to call a server method – User2012384 Jun 25 '15 at 08:58
  • To commentors: you may want to google [PageMethods](http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2), which is a framework which wraps the AJAX call. – John Wu Jun 25 '15 at 09:08

1 Answers1

0
[WebMethod]                               
  public static void ValidatePage()      //in aspx.cs
  {
     //Your Logic
  }



function Validate() {
        event.preventDefault();
        $.ajax
            ({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(argument),
                datatype: "json",
                url: yoururl + "/ValidatePage",
                success: successhandler,
                error: errorhandler
            });
    }

Something like this.The response you get will be handeled in successhandler

AkshayJ
  • 771
  • 6
  • 15