简体   繁体   English

Javascript调用Web服务和ASP.Net序列

[英]Javascript calling webservice and ASP.Net Sequence

I`m using Javascript function that uses a web service and after finishing it i have to update a Grid Datasource 我正在使用使用Web服务的Javascript函数,完成后我必须更新Grid Datasource。

To accomplish this task i called the javascript function via RegisterClientScriptBlock then call the UpdateGridDataSource method 为了完成此任务,我通过RegisterClientScriptBlock调用了javascript函数,然后调用了UpdateGridDataSource方法

But While Debugging i found that it calls the UpdateGridDataSource method first .. Then Call the method in the WebService! 但是在调试时,我发现它首先调用UpdateGridDataSource方法。然后在WebService中调用该方法!

ASP.Net Code ASP.Net代码

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

UpdateDataSource(); 

JavaScriptCode JavaScript代码

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) 问题在于,在DeletePOI(string ID)之前调用UpdateDataSource()

Javascript code registered with RegisterClientScriptBlock() runs on the client in the browser. RegisterClientScriptBlock() Javascript代码在浏览器的客户端上运行。 ASP.NET code runs on the server. ASP.NET代码在服务器上运行。

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() . 它说,当页面加载到客户端上时,调用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. 完成所有ASP.NET代码后,它会将页面发送到客户端。 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. 您不能在服务器端ASP.NET代码中间调用客户端Javascript并等待其返回。

What you can do is call the web service directly from your server-side code. 您可以做的是直接从服务器端代码调用Web服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM