简体   繁体   English

在C#中从服务器端异步调用javascript函数

[英]Calling a javascript function asynchronosly from server side in c#

this is my code : 这是我的代码:

protected void BtnImport_Click(object sender, EventArgs e)
{
   List<DataTable> tb = Helper.SplitTableToManyTables(FileTb, 50); // a list containing multiple DataTable each have 50 rows
        int importedRowsCount = 0;
        for (int KLoop = 0; KLoop < tb.Count; KLoop++)
        { 
        ...
   if (QueriesDataHelper.ImportContacts(resTb, int.Parse(TxtHiddenGroupId.Value), Session)) 
            {
                importedRowsCount += resTb.Rows.Count;
                var script = "DisplayProgressMsg(" + importedRowsCount + ")";
                ScriptManager.RegisterStartupScript(this, GetType(), "MyScript", script, true);
                if (KLoop == tb.Count - 1)
                    MessageBox.Show(importedRowsCount.ToString()+" Contacts imported successfully");
            }
    }
}

QueriesDataHelper.ImportContacts is a function that take a dataTable containing 50 rows and send it to a stored procedure QueriesDataHelper.ImportContacts是一个函数,该函数获取包含50行的dataTable并将其发送到存储过程

my problem is RegisterStartupScript is beeing executed after all the dataTables are beiing inserted i want to display a ms after the first DataTable finish tha 我的问题是在所有数据表都插入后我要执行RegisterStartupScript,我想在第一个数据表完成后显示ms

Ok, 好,

Http is request/response protocol. Http是请求/响应协议。 But you want send do multiple responses for single request. 但是您要发送对单个请求的多个响应。

I see two ways for you. 我为您看到两种方式。 In both, you will have to run your long running process in separate thread. 在这两种方法中,您都必须在单独的线程中运行长时间运行的进程。 Then 然后

1) use ajax, to ask server about progress every N seconds, and display result. 1)使用ajax,每隔N秒向服务器询问进度,并显示结果。 Client's will track progress with some delay 客户的进度会有所延迟

2) web sockets( SignalR is great for such purposes). 2)Web套接字( SignalR非常适合此类用途)。 In this way you will be able to display progress in real time, but i think it's harder to implement 这样,您将能够实时显示进度,但是我认为很难实施

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

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