0

I`m using Javascript function that uses a web service and after finishing it i have to update a Grid Datasource

To accomplish this task i called the javascript function via RegisterClientScriptBlock then call the UpdateGridDataSource method

But While Debugging i found that it calls the UpdateGridDataSource method first .. Then Call the method in the WebService!

ASP.Net Code

System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "delete", "DrawPOI();", true);

UpdateDataSource(); 

JavaScriptCode

function DeletePOI(arg) {
      //Some Code  
        $.ajax({
            url: "JSBLL.asmx/DeletePOI",
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{id:'" + arg + "'}",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (e) {
                alert('Error');
            }
        }); //some code
        return false;
    }

WebService :

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string DeletePOI(string ID)
        try
            {
                if (id == "")
                    throw new Exception();

                long ID = Convert.ToInt64(id);
                using (GuardEntities database = new GuardEntities())
                {
                    var poi = database.POIs.Where(x => x.ID == ID).FirstOrDefault();

                    database.POIs.DeleteObject(poi);
                    database.SaveChanges();
                }

                return "POI Deleted Successfully";
            }
            catch
            {
                return "Error Occured";
            }
}

The Problem is that UpdateDataSource() is called before DeletePOI(string ID)

5
  • put some code to understand the problem Commented Jan 25, 2013 at 6:45
  • The problem is in the sequence of firing events if u didn`t notice :) Commented Jan 25, 2013 at 6:46
  • Code Added To The Question Commented Jan 25, 2013 at 6:55
  • What does the UpdateGridDataSource do? Commented Jan 25, 2013 at 7:00
  • Just Query The Data From The Server And Bind It To The Grid Commented Jan 25, 2013 at 7:07

1 Answer 1

0

Javascript code registered with RegisterClientScriptBlock() runs on the client in the browser. ASP.NET code runs on the server.

When it gets to the line:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "delete", "DrawPOI();", true);

It says, when the page loads on the client, call DrawPOI(). It then immediately runs the next line of code:

UpdateDataSource(); 

When it's done with all the ASP.NET code, it then sends your page to the client. Basically, what you are trying to do will not work. You can't call client-side Javascript in the middle of your server-side ASP.NET code and wait for it to return.

What you can do is call the web service directly from your server-side code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.