1

Hi i have the following pagemethod, however it dues not seem to be working, i tried debugging it and it does not hit the method. Here is what my method looks like;

function InsertStatus() {

  var fStatus = document.getElementById('<%=txtStatus.ClientID %>').value;
  PageMethods.InsertStatusUpdate(fStatus, onSucess, onError);

  function onSucess(result) {
      alert(result);
  }

  function onError(result) {
      alert('Cannot process your request at the moment, please try later.');
  }
}

And my codebehind;

    [WebMethod]
    public static string InsertStatusUpdate(string fStatus)
    {
        string Result = "";
        int intUserID = -1;

        if (String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
          HttpContext.Current.Response.Redirect("/login");
        else
            intUserID = Convert.ToInt32(HttpContext.Current.User.Identity.Name);

        if (string.IsNullOrEmpty(fStatus))
            return Result = "Please enter a status";
        else
        {

            //send data back to database

            return Result = "Done";

        }

    }

When i click my button it goes straight through the onError Method. Can anyone see what i am doing wrong? I found the problem i needed a [System.Web.Script.Services.ScriptService] above the method, due to the fact it is being called by a script. Thanks for all the suggestions.

pmillio
  • 1,089
  • 2
  • 13
  • 20
  • Have a look at this http://stackoverflow.com/questions/2521352/calling-asp-net-code-behind-function-from-javascript/2521409#2521409 – Muhammad Akhtar Jun 20 '11 at 16:14
  • 1
    Do you have a script manager on the page with client methods enabled? That might be why your breakpoint on the server never gets hit. – Brian Dishaw Jun 20 '11 at 16:35

2 Answers2

1

If I were to guess, I would focus on this:

 intUserID = Convert.ToInt32(HttpContext.Current.User.Identity.Name); 

The best way to solve this is set a breakpoint and start walking through the code. When you run a line and are redirected to the error page, you have found your problem.

The reason I picked that line, is the user is a string. Now, it may be your users are numbers, but it could also be including a domain user == "mydomain/12345", which is not an integer, even if the user part of the string is.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
1

As far as I know, you can't Response.Redirect in a PageMethod.

Return a string of the redirect URL and then use JavaScript document.location.href to handle the redirection.


EDIT: I've just seen that you tried debugging and the method isn't hit: ensure your ScriptManager has EnablePageMethods set to true:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>
Town
  • 14,706
  • 3
  • 48
  • 72
  • Hi i have set the enablepagemethods, still nowthing, it is in a masterpage does this matter? – pmillio Jun 20 '11 at 19:39
  • [Yes](http://weblogs.asp.net/muhanadyounis/archive/2008/12/30/scriptmanager-and-masterpage-pagemethods.aspx). – Town Jun 21 '11 at 08:51