2

I've been trying to call a c# function in javascript this way :

function x() {
    PageMethods.F1(onSucess, onError);

    function onSucess(result) {
       alert('Success');
    }
    function onError(result) {
       alert('Something wrong.');
    }
}
[WebMethod]
public static boolean F1()
{
    return true;
}

 

<asp:Button ID="Button1" runat="server"  OnClientClick="x(); return false"    Text="Button"  />

But I don't get any result, I mean neither of the alert messages pops up. What exactly is wrong ?

8
  • Do you get a js error in the console? Commented Aug 3, 2013 at 20:48
  • @Mario : No, I get nothing, the button doesn't do anything at all. Commented Aug 3, 2013 at 20:50
  • @Mario: What would be the best way to call a c# function in js ? Commented Aug 3, 2013 at 20:52
  • Try public static string F1() { return "Hello"; } instead, see if that works. Commented Aug 3, 2013 at 20:55
  • @Mario: No it doesn't work :D. Commented Aug 3, 2013 at 20:58

1 Answer 1

1

Set EnablePageMethods to true on the ScriptManager.

<asp:ScriptManager EnablePageMethods="True" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.