pmillio
pmillio

Reputation: 1139

Calling Pagemethod

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.

Upvotes: 1

Views: 802

Answers (2)

Town
Town

Reputation: 14906

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"/>

Upvotes: 1

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

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.

Upvotes: 1

Related Questions