1

I am trying to call a method that is in a User Control from clientside using ajax/jquery. My ajax looks something like this:

function starClick(starIndex) {
    $.ajax({
        type: "POST",
        url: "ItemPage.aspx/postRatingProxy",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            document.getElementById("testAjax").innerHTML = msg.d;
        }
    });
}

My page method look something like:

[WebMethod]
public static string postRatingProxy()
{
    return .......postRating();
}

Then the User Control Method looks something like:

public static string postRating()
{
    return "git er done";
}

I saw this method being suggested somewhere. Although Im very lost as to how to retrieve my UserControl method from the Page method when its static. Is it possible to retrieve the UserControl from the static method or did I just run into a dead end?

Ozzy We
  • 73
  • 2
  • 8

1 Answers1

0

Is it possible to retrieve the UserControl from the static method or did I just run into a dead end?

No, this is not possible. An ASP.NET PageMethod is static and doesn't give you access to any user controls. The reason for this is simple. When you do an AJAX request with jQuery there's no ViewState being sent to the server and thus the notion of User Control hardly makes sense. If you need to access some value in the Page Method have this value being sent as parameter in the AJAX request:

data: JSON.stringify({ someParameter: 'some value you could take from wherevr you want' }),
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • If your right then somebody else lied to me. Finger point at -> http://stackoverflow.com/questions/3392345/using-jquery-ajax-to-call-asp-net-function-in-control-code-behind-instead-of-pag – Ozzy We May 11 '13 at 17:06
  • 1
    Feel so dumb but I figured it out. Once I took some time to chill from the problem I came back and figured it out right away. Basically all I had to do was: call return USERCONTROLNAME.postRating(); since its static you can access it directly from the class. Fail on my part sorry for the bother. – Ozzy We May 11 '13 at 17:34