0

I'm working with a VB.Net page which calls a web method from JavaScript, this was working upto a week ago and now for some reason is not working. I was wondering if anyone could give me any idea where to look as I am completely lost right now.

Firstly my page generates a list of items that can be clicked, The line that does this is:

 TicketHTML = TicketHTML + "<td><img src='../images/delete.png' Class='imgTicketClose'  alt='Delete Task' onclick='DeleteTicket(" + row("id").ToString() + ")' /></td></tr>"

I know this is working as when the item is clicked I get a javascript popup so I would assume the problem is not here.

Now my Javascript:

    function DeleteTicket(ticketID) 
     {
        var answer = confirm("Do you really want to delete this task?")
        if (answer) 
        {
           PageMethods.DeleteTask(ticketID);
           window.location.reload()
        }
     }

I assume the problem is here as the web method never seems to call and the page doesnt seem to reload either, but I dont understand why this would be failing if the javascript displays the confirm dialog.

In the interest of completeness here is my web method though I know this is not being called as I have profiled the database.

 <System.Web.Services.WebMethod()>
   Public Shared Sub DeleteTask(ticketID As Integer)
  Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())

  Dim cmd As New SqlCommand
  cmd.CommandType = CommandType.StoredProcedure
  cmd.CommandText = "spDeleteNonTicketItem"
  cmd.Parameters.AddWithValue("@ItemID", ticketID)

  cmd.Connection = conn

  conn.Open()

  cmd.ExecuteNonQuery()

  conn.Close()

   End Sub

So where could I look? Any advice would be greatly appreciated. Thanks

EDIT: Could anything in the web.config file be preventing page methods from executing? I have just noticed this happening with a completely seperate web method and have checked the integers being passed into the javascript with an alert() and they are all valid

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59
  • You can execute VB functions in javascript? :O – Nick Shvelidze Dec 14 '11 at 14:35
  • @Shvelo yep :D I had this working a while back but no idea why it doesnt work anymore – Purplegoldfish Dec 14 '11 at 14:36
  • 1
    In C# you need to mark your method as [WebMethod], don't know about VB – Erix Dec 14 '11 at 14:36
  • You might want to try putting a breakpoint in your function to make sure it isn't being called. If the function is called, but throws an exception before it reaches the conn.Open() function, watching your DB profiler tells you nothing. – mikemanne Dec 14 '11 at 14:37
  • 2
    Is DeleteTask decorated with the attribute? It needs to be to be "reachable" from the client. Has this been removed recently? Also, are there any javascript errors? – dash Dec 14 '11 at 14:37
  • "but I dont understand why this would be failing if the javascript displays the confirm dialog." for one thing, if answeris false it won't execute. – Erix Dec 14 '11 at 14:38
  • @Purplegoldfish: Could you put a breakpoint at the beginning of DeleteTask method and see if it stops there when called? :D – Răzvan Flavius Panda Dec 14 '11 at 14:39
  • @Erix yes but the answer is not false, since the dialog always displays and I click the ok option there is no reason it would ever fail the check – Purplegoldfish Dec 14 '11 at 14:39
  • You might also want to use Firebug (or a similar tool) to ensure you're reaching the PageMethods.DeleteTicket() call. Also, make sure the passed ticketId is a valid integer. Not sure what your DeleteTicket vb function will do if passed null or a non-integer value, but it most likely won't be good. – mikemanne Dec 14 '11 at 14:39
  • Sorry about the web method not showing up, SO had hidden it due to the < > brackets for some reason, I have updated the code to display properly, and in the enviroment im working I cant breakpoint the system without getting it working locally which id like to avoid right now – Purplegoldfish Dec 14 '11 at 14:41
  • @Purplegoldfish I know it seems trivial, but do yourself a favor and put a breakpoint there to make sure its true. – Erix Dec 14 '11 at 14:41
  • Also check your web.config; needs to be set for the error message to be returned to the client. – dash Dec 14 '11 at 14:56
  • @dash thanks, just checked and it is deffinately off – Purplegoldfish Dec 14 '11 at 14:59
  • See additional edits to my answer for some additional checks. Did you release recently? – dash Dec 14 '11 at 15:04

2 Answers2

2

Are you able to confirm that its trying to execute the PageMethod, like when you set a breakpoint there is it hitting it?

I dont know if this is causing you problems, but I did see this:

function DeleteTicket(ticketID) 
 {
    var answer = confirm("Do you really want to delete this task?");
    if (answer) 
    {
       PageMethods.DeleteTask(ticketID);
       window.location.reload();
    }
 }

I wonder if adding the semi-colons is causing you an issue.

Etch
  • 3,044
  • 25
  • 31
  • Just saw the missing javascript semicolons - good catch. I bet you a shiny new nickel that's what is preventing the JS function from performing as expected. – mikemanne Dec 14 '11 at 14:44
  • 2
    Believe it or not, the semi-colons are optional. Doesn't mean you shouldn't use them though. See http://mislav.uniqpath.com/2010/05/semicolons/ – dash Dec 14 '11 at 14:51
  • Good call, but sorry adding the javascript semicolons didnt help :( – Purplegoldfish Dec 14 '11 at 14:51
1

EDIT - I can see you have added the WebMethod attribute.

One thing you can do is add an error handler to your page methods call. You can usually supply method handlers for onsuccess and onError.

For example, you can add the following javascript method:

function OnFailed(error) {
   // Alert user to the error.
   alert(error.get_message());
}

And then, in your PageMethods call:

    function DeleteTicket(ticketID)    
    {      
        var answer = confirm("Do you really want to delete this task?");      
        if (answer)       
        {         
            PageMethods.DeleteTask(ticketID, null, OnFailed);         
            window.location.reload();      
        }   
    } 

Just found a StackOverflow question with an example of this. Note also the effects of CustomError in web.config.

Further Edit.

Have you done a release recently?

You need to make sure that:

1) On the ScriptManager of your page, EnablePageMethods="true" is set

2) Web.config should have the following lines in the relevant sections:

<system.web>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" 
type="Microsoft.Web.Script.Services.ScriptHandlerFactory" 
validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web> 
Community
  • 1
  • 1
dash
  • 89,546
  • 4
  • 51
  • 71
  • Thanks for the answer, the WebMethod attribute just got hidden by SO because of the < > brackets. I have added it back into my example now, Also there are no errors at all. I cant debug locally without a massive change to hardcode in a lot of values so its quite hard to debug this project properly but im very sure its not hitting that method – Purplegoldfish Dec 14 '11 at 14:48
  • Just checked the integers being passed in and they are deff valid, I have also seen this happening with another web method now, both were working a week or so ago, so im wondering if something could be missing from web.config that allows these to work? – Purplegoldfish Dec 14 '11 at 14:56
  • Thanks dash. It was the script manager, my boss has done some optimising and added a master page to the section im working on, he had deleted my script managers and added one in the master page but it was missing the EnablePageMethods attribute. – Purplegoldfish Dec 14 '11 at 15:27