0

I have an ASPX page that calls the server with an AJAX call that is inside a javascript function. I need to process the (json) data that is sent to the server via and then return data from the server back to the client and call a function with parameters.

My ajax code

function () {
        var myVar= "ooooblah";
       
        $.ajax({
            type: "POST",
            url: "MyPage.aspx/TestFunc",
            data: "{val:'" + myVar + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess
    });
}

My code behind aspx.cs

[WebMethod]
public static void TestFunc(string val)
{
    //process code here
    Page.ClientScript.RegisterStartupScript(...); //tried using this but it don't work
}

The error I get when I try to compile is :

Error CS0120 An object reference is required for the non-static field, method, or property 'Control.Page'

Error CS0026 Keyword 'this' is not valid in a static property, static method, or static field initializer

Do I need to put it outside in the Page_Load? Not sure how to fix this

Community
  • 1
  • 1
fifamaniac04
  • 2,343
  • 12
  • 49
  • 72

1 Answers1

0

You won't be able to call a javascript function from C# because that's a "stateless" (lets say) POST call, and that call runs outside the page context. That method could be in any other class and the result would be the same, you won't have access neither to the Controls array nor the Page properties, that's why the method is static.

What you can do is to add a more complex response as a hint to the OnSuccess function. So the OnSuccess function could check: "Hey if I get an ExtraArgsForMagicFunction property in the response it means that I need to call my magic function"

On the Client

<script type="text/javascript">

    var myVar= "ooooblah";

    $.ajax({
        type: "POST",
        url: "WebMethodTest.aspx/TestFunc",
        data: "{val:'" + myVar + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess
    });

    function onSuccess(data) {
        alert(data.d.MainResponse);

        if (data.d.ExtraArgsForMagicFunction) {
            magicFunction(data.d.ExtraArgsForMagicFunction)
        }
    }

    function magicFunction(args) {
        alert(args[0]);
    }
</script>

On the server

[Serializable]
public struct FuncResult
{
    public string MainResponse;
    public string[] ExtraArgsForMagicFunction;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static FuncResult TestFunc(string val)
{
    return new FuncResult()
    {
        MainResponse = val + "Response",
        ExtraArgsForMagicFunction = new[] { "Some Extra Args" }
    };
}

If you get a 401 check this out ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"

If you want to know "Why d?" check this out What does {"d":""} means in asp.net webservice response

Community
  • 1
  • 1
hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • Thanks but I don't think you understood my question... I am able to go from JavaScript to C# (codebehind) ... what I'm having problem doing is calling a separate javascript function when I'm in C#... I can hit the success just fine – fifamaniac04 May 07 '17 at 04:56
  • Thanks but I was able to get the ajax working...Turns out I wasn't formatting JSON data correctly – fifamaniac04 May 09 '17 at 05:23