简体   繁体   English

异步asp.net表

[英]Async asp.net table

I am populating an asp.net table (up to 64 rows) with data from a SQL query and multiple web requests (1 per row). 我使用来自SQL查询的数据和多个Web请求(每行1个)填充一个asp.net表(最多64行)。 The first 4 columns are populating quickly, however the last 2 take 6 seconds per web request. 前4列快速填充,但最后2列每个Web请求需要6秒。 I would like the last 2 columns to display a loading gif and update once the web calls are completed. 我希望最后2列显示加载gif并在Web调用完成后更新。 Because credentials are passed in the web call I would like to make the calls in the server side. 因为凭据是在Web调用中传递的,所以我想在服务器端进行调用。

Additionally, I would like to make multiple web requests in parallel. 另外,我想并行提出多个Web请求。 I am looking into tasking, however I am not sure how to allow the table to complete while tasking specific columns. 我正在研究任务,但是我不确定如何在执行特定列时完成表格。

I'm not attached to the way I am doing this, however I'm still new to programming and am most familiar with C# and asp.net. 我并不喜欢我这样做的方式,但我仍然是编程新手,对C#和asp.net最熟悉。

Right now it looks something like: 现在它看起来像:

Ajax Page Ajax Page

<div>
    <form id="ajaxForm" name="ajaxForm" runat="server">
        <asp:table id="aspTbl" runat="server" />
    </form>
</div>

With the c# being: 随着c#是:

//SQL Connection
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["webConfigConnection"]);
con.Open();
SqlCommand cmd = new SqlCommand("Select name, type, location, IP from tbl", con)
SqlDataReader sdr = cmd.ExecuteReader();

while (sdr.Read()
{

    //web requeststring
    sURL;
    sURL = "http://" + sdr.GetValue(4).ToString()  + "WebRequestURL";
    WebRequest request;
    request = WebRequest.Create(sURL);
    request.Method = "GET";
    WebResponse response = request.GetResponse();
    StreamReader Report = new StreamReader(response.GetResponseStream());
    string ReportString = Report.ReadToEnd().ToString();
    Response.Close();

    //Populate Table
    TableCell tc1 = new TableCell();
    TableCell tc2 = new TableCell();
    TableCell tc3 = new TableCell();
    TableCell tc4 = new TableCell();
    tc1.Text = sdr.GetValue(0).ToString();
    tc2.Text = sdr.GetValue(1).ToString();
    tc3.Text = sdr.GetValue(2).ToString();
    tc4.Text = sdr.GetValue(3).ToString();
    tc5.Text = ReportString.SubString(paramaters);
    tc6.Text = ReportString.SubString(other paramaters);
    TableCell[] tcRow = new TableCell[] { tc1, tc2, tc3, tc4, tc5, tc6 };
    tr.Cells.AddRange(tcRow);
    asptbl.Rows.Add(tr);

}

The goal is to get T5 and T6 to initially populate with the loading GIF and update asynchronously with the substring once available. 目标是让T5和T6最初填充加载GIF并一旦可用就与子字符串异步更新。

Edit: 6/10/2015 编辑:6/10/2015

Thanks for the advice. 谢谢你的建议。 It works for running the web requests in parallel, however it still requires them to all complete before posting, which I realized over the weekend is not something I can avoid running this on the server end. 它适用于并行运行Web请求,但是它仍然要求它们在发布之前全部完成,我在周末意识到这不是我可以避免在服务器端运行它的东西。 What I'll be trying is to have the cells make a jquery ajax call for the web request and update the cells so that the columns 1-4 can load instantly and columns 5 and 6 will populate once available. 我将要尝试的是让单元格对Web请求进行jquery ajax调用并更新单元格,以便列1-4可以立即加载,第5列和第6列将填充一次可用。 Will provide an update with how it works. 将提供有关其工作原理的更新。

Edit: 6/10/2015 #2 编辑:6/10/2015#2

I updated to populate the cells empty and upon completion of the ajax call that hits calls this, I loop through the cells to populate them with javascript. 我更新了将单元格填充为空,并且在完成调用此调用的ajax调用后,我遍历单元格以使用javascript填充它们。 The issue I ran into now is that the cell that I am trying to populate is calling the same form a large number of times simultaneously and the web requests are queuing up. 我现在遇到的问题是我尝试填充的单元格同时调用相同的表单并且Web请求正在排队。

Just if its relevant, 只要相关,

List<Task> TaskList = new List<Task>();   

Task PushRow(Table tbl, string name,string type,string location,string IP ){ 
    Task.Run(()=>{

       //Add code for webdownload and table row adding

    });
}

Then call this function inside while loop and store the result in a list TaskList of type List<Task>. 然后在while循环中调用此函数,并将结果存储在List <Task>类型的列表TaskList中。 Use 采用

Task.WaitAll(TaskList.ToArray());

To wait for all task to be completed in after the loop. 等待循环后完成所有任务。

I ended up creating the table with empty web request cells. 我最终创建了具有空Web请求单元格的表。 Once the page loads, the client calls the web request for each cell and updates the cells. 页面加载后,客户端调用每个单元格的Web请求并更新单元格。 The only thing I couldn't figure out was how to update 2 cells with one web call, but the solution in place works. 我唯一想知道的是如何通过一次Web调用更新2个单元格,但是现有的解决方案可行。

Thanks for taking a look. 谢谢参观。

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

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