How can I load ASP.NET GridView from multiple sources asynchronously?

Here is the situation. I need to press ~ 50 servers and capture some data from a file. Then I want to display several rows for each ASP.NET GridView control.

I tried to do this with Threads / ThreadPool and successfully collect all the data in the session.

What I would like to do, and what is difficult for me to determine, is updating the grid for the user after loading each server.

If I put the data binding code in the stream, it will only display what was loaded at the time the response was sent to the client. If I select this from a thread, I will have to wait until all the threads have completed to send a response, and this does not do what I want.

Does anyone have any ideas? I see something about Asynchronous client callbacks , but I'm not sure if this is what I need to use. I don't know how to manipulate a GridView from Javascript.

Thanks for any help you can provide.

+4
source share
4 answers

There is no way for server side ASP.NET code to call a web browser and notify it of new data. Therefore, you will have to use JavaScript to poll the server for new data. There are various ways to do this. As an example, we could use Server-side page Methods , which could tell the client if there is more data and when all the data has been loaded.

[WebMethod] public static bool IsNewDataAvailable(int currentClientRows) { return dataCollectedSoFar.Count > currentClientRows; } [WebMethod] public static bool IsFinished() { // return true if all the threads in the thread pool are finished } 

You need to call the IsNewDataAvailable method at regular intervals. A simple JavaScript timer should do the trick.

When new data appears, you need to redisplay the GridView. Again, there is more than one way to do this, but a nice and easy way would be to include the GridView in the UpdatePanel next to the button with the style = "display: none;" to keep it hidden. Then, if there is new data, simply call the JavaScript click method on the button to refresh the contents of the update panel.

 <script> var timerId = setInterval("checkForData()", 5000); function checkForData() { // If all threads have finished, stop polling if (PageMethods.IsFinished()) { clearInterval(timerId); } var currentRowCount = 0; // Find out how many rows you currently have, if // you have jQuery you could do something like this currentRowCount = $("#<%= myGridView.ClientID %> tr").length; if (PageMethods.IsNewDataAvailable(currentRowCount)) { // Here we trigger the hidden button click method, again // using a bit of jQuery to show how it might be done $("#<%= myHiddenButton.ClientID %>").click(); } } </script> . . . <asp:UpdatePanel ID="myUpdatePanel" runat="server"> <ContentTemplate> <asp:GridView ID="myGridView" runat="server"> . . . </asp:GridView> <asp:Button ID="myHiddenButton" runat="server" style="display: none;" OnClientClick="myHiddenButton_Click" /> </ContentTemplate> </asp:UpdatePanel> 

Finally, to populate the server-side GridView, you can continue to use ThreadPool and simply visualize all the data that you have every time. For instance:

 protected void myHiddenButton_Click(object sender, EventArgs e) { myGridView.DataSource = dataCollectedSoFar; myGridView.DataBind(); } 
+3
source

I rarely use ASP.net controls, so it's hard for me to answer this question. I will say that I would use Ajax to tell the client when to restart Gridview after all the server data has been restored.

I read a little about something called active widgets , which will replace your gridview.

Here's how I would handle it: with the initial design, create an event mechanism that runs in each boot task of each server and stores the results in a session. On the client side, continue to update the update object if not all server requests are complete. A great data format for this would be JSON.

Anyway, I hope this helps.

0
source

You can only use gridview in UpdatePanel and handle client-side callbacks from there, which is probably the easiest implementation for gridview. Basically, you just wrap the gridview with an update panel, and everything inside will work using ajax. You have to write efficient code to query your data, but ui should be simpler.

You can do all this manually using Javascript and JSON and a client web service, but first I will try the update panel.

0
source

protected void load_Click (object sender, EventArgs e) {sqc.ConnectionString = dbc.sqlconn (); sqc.Open (); OleDbCommand cmd = new OleDbCommand ("SELECT cus_serial, cus_name, cus_mobile, no_person, unit_cost, total_cost from booking", sqc); // OleDbDataAdapter adapter = new OleDbDataAdapter (cmd);

  sdr = cmd.ExecuteReader(); DataTable dt = new DataTable("A"); dt.Load(sdr); GridView1.DataSource = dt; GridView1.DataBind(); sdr.Close(); sqc.Close(); } 
0
source

All Articles