0

So I need to make a list of items with delete buttons delete their specific item. I set up an Ajax call and a webmethod in the codebehind, as I've done before in this site, but for some reason it returns the page's entire HTML again, rather than executing the webmethod and returning what I'd like it to return. Frustrated I tried to use a pagemthod instead, but it returns the same result without executing my webmethod at all.

Interestingly I have this working on several other pages already, and basically copy and pasted what I had, but for some reason on this page it doesn't work. Which means I know that it's not an issue with the server or any httpModules in the web config. I've tried the solutions I used before on my site, which were:

form1.Action = Request.RawUrl; at the top of the Page_Load function in codebehind, because of URL rewriting

PageMethods.set_path("~/Inbox.aspx"); in the javascript for the same reason as above

The below was a proposed solution to go in the web.config, but it just causes the whole server to 500. I don't see how it could help anyway though since pagemethods and ajax work on other pages on my site.

<system.web>
  <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </httpModules>
</system.web>

I've stripped the code down to almost nothing to make it function as basically as possible, so I'm certain there are no errors. The code always executes as successful anyway.

Here's the full code, just for context:

HTML/JS:

<form id="form1" runat="server" style="text-align:left">
        <asp:ScriptManager runat="server" ID="mailSM" EnablePageMethods="true" EnablePartialRendering="true" ClientIDMode="Static" ></asp:ScriptManager>
        <input runat="server" id="maillist" type="hidden" value="" />
        <input runat="server" id="inboxhf" type="hidden" value="1" />
        <div id="mail-collection" class="main-container">
            <a id="back" class="buttons" href="/">back</a><a class="buttons" href="/sendmail">send new mail</a><a id="box" class="buttons" href="/outbox">your sent mail</a>
        </div>

        <script type="text/javascript">
            function DeleteMail(mailID, fromID, toID) {
                let pagepath = window.location.pathname.substr(1);

                PageMethods.set_path("Mailbox.aspx");
                PageMethods.DeleteMailCB(fromID, toID, pagepath, mailID, OnSuccess, OnError);
            }

            function OnSuccess(response) {
                alert(response);
                location.reload();
            }

            function OnError(response) {
                alert("e" + response);
                //OnSuccess();
            }
        </script>
    </form>

C#:

[WebMethod]
public static void DeleteMail(string fromID, string toID, string pagepath, string mailID)
{
    DB.TestForSomething();
    //above just does a basic INSERT straight into a test table in the 
    //database. since nothing is being inserted, I know this webmethod 
    //isn't getting used at all.

    return;
}

Not sure what I should do from here since the solutions that I've found work on the other pages, but not this one. Thanks in advance!

el toro
  • 532
  • 4
  • 11
  • You have to turn on [`EnablePageMethods`](https://stackoverflow.com/a/4558607/4228458) – CodingYoshi Feb 17 '19 at 23:48
  • Yes I have a ScriptManager on the page, `` is there. – el toro Feb 17 '19 at 23:51
  • I got the httpmodule thing I mentioned earlier working, but it didn't help at all. Some other solution said to change `settings.AutoRedirectMode = RedirectMode.Permanent;` to `settings.AutoRedirectMode = RedirectMode.Off;`, but that didn't work either. I have no idea what to do and this question was drowned, @CodingYoshi please help me – el toro Feb 18 '19 at 00:55
  • You're saying you have `EnablePageMethods` turned on but I don't see it in the code you have posted. How come? Also, if I were you I would create a brand new project and only add this one method and do not connect to db or anything and see if your approach works. – CodingYoshi Feb 18 '19 at 01:29
  • I didn't post all the HTML, just the JS section involved and forgot the script manager. There's lots of code on the page so it would go on quite a bit if I posted the whole thing. Sorry about that. I did create a whole new ASPX page to start over but it still returns the HTML instead of executing the webmethod, which is infuriating since it works on other pages. I don't need to create a new project to know this works, since the same method works elsewhere in the same project. – el toro Feb 18 '19 at 02:44
  • @torogadude You said you were not able to hit your method in the code behind. Can you please see if you can do this? You have "models" that work, as you say, so you'll get it. – kblau Feb 20 '19 at 17:58
  • @kblau I edited the post to include the full HTML for that page. – el toro Feb 21 '19 at 02:11
  • @kblau If you'd like to see a working example of it, go to the forum on my site (https://edudagorot.com/forum) and look at the page source JS for the vote arrows and post deleting buttons. They're exactly the same, and the webmethods for those pages are set up pretty basically. – el toro Feb 21 '19 at 02:14
  • @kblau OKAY HOLD THE PHONE, I think it's broken on the whole website, because now that it's back online, the voting and deleting and anything called by a webmethod indeed no longer works, site-wide. I'm trying to remember what could've caused this, because I may have screwed with some background settings recently, but I'm not entirely sure. I know the code is sound since it's untouched though. Any ideas? – el toro Feb 21 '19 at 02:17
  • @torogadude good luck – kblau Feb 22 '19 at 17:48

2 Answers2

0

Here I am hitting my method. Please see comments in the code.

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="FredWebForm.WebForm4" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script type="text/javascript">
        function DeleteMail(mailID, fromID, toID) {
            let path = window.location.pathname.substr(1);
            //"inbox" or "outbox"

            //changed path for me
            //PageMethods.set_path("~/Inbox.aspx");<---did not work
            //What you had did not work
            PageMethods.set_path(path);
            PageMethods.DeleteMail(fromID, toID, path, mailID, OnSuccess, OnError);
        }
        function OnSuccess(response) {
            alert(response);
            //remarked out initially
            location.reload();
        }
        function OnError() {
            //OnSuccess();
        }
        $(function () {
            $("#theButton").click(function () {
                DeleteMail(12, 14, 16);
            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="sm" EnablePartialRendering="true" EnablePageMethods="true">
        </asp:ScriptManager>
        <div>
            <input type="button" value="Go" id="theButton" />
        </div>
    </form>
</body>
</html>

Code behind

//I am using WebForm4, but you can use a different page name
public partial class WebForm4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        form1.Action = Request.RawUrl;
    }

    [WebMethod]
    public static string DeleteMail(string fromID, string toID, string pagepath, string mailID)
    {
        //DB.TestForSomething();
        //above just does a basic INSERT straight into a test table in the 
        //database. since nothing is being inserted, I know this webmethod 
        //isn't getting used at all.

        return "SomeData";
    }
}
kblau
  • 2,094
  • 1
  • 8
  • 20
  • When I do `PageMethods.set_path(path);` instead of what I had before, I get an error in the console. The path needs to be the actual .aspx path, not the URL rewritten path, or else it can't find the webmethod. It's the same thing I had to do when using webmethods before. I'm certain my code is sound but it just doesn't work no matter what I do, even though it works on separate pages in the same project, and I don't know what to do about it. Even deleted and remaking the page doesn't fix it. It's blowing my mind – el toro Feb 20 '19 at 02:51
0

To anyone who stumbles upon this from the future, who knows they did everything correctly but still has it refuse to work: I found, way at the bottom of my web.config, buried beneath all of my URL rewrites, a small little <modules><remove name=""></modules> rule that disabled the ScriptModule.

Not sure how that got there, but apparently it did. As soon as I removed it, the site's webmethods started working again. I feel stupid that I was stuck on this for a whole week.

el toro
  • 532
  • 4
  • 11