tags:

views:

707

answers:

2

I have a web page where it will input an excel/CSV file from the user and read the data from it and import to DB.While inserting each record.I just want to show the details about the record being inserted to the user.(Ex : Client Details of A is adding...)

+2  A: 

Try this... Set the output to unbuffered (Response.BufferOutput), and include some javascript in your page that updates the UI as you see appropriate. For example, it might update a SPAN with a percentage complete or the details of the record you are processing. Then in your server code, output <script> tags that call the Javascript function from the Render override. Make sure you call Flush() at the appropriate times, and also Flush the base code after it Renders... The JS function calls should get sent down at the appropriate times and executed on the client, resulting in an updating page.

For example... Your HTML page might look like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function UpdateScreen(t) {
         document.getElementById('output').innerHTML = t;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <div id='output'></div>
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    UpdateScreen('hello');
</script>

and your codebehind will look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
     protected void Page_Load(object sender, EventArgs e)
     {

     }

     protected override void Render(HtmlTextWriter writer)
     {
      Response.BufferOutput = false;

      base.Render(writer);
      Response.Flush();

      for (int i = 0; i < 10; i++)
      {
       Thread.Sleep(1000);
       Response.Write(string.Format("<script type='text/javascript'>UpdateScreen('{0}');</script>", i * 10));
       Response.Flush();
      }
     }
    }
}
Michael Bray
why should i override render method ? i need to show the progress while my for loop iterates each time
Shyju
If you do it during Page_Load, then the main content hasn't been sent to the client. That's why we still have to call base.Render(...) in our override - to get the real content to the client. Render is basically the last part of the page lifecycle to get executed.
Michael Bray
Here's a helpful overview of the page lifecycle: http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg
Michael Bray
A: 

I know this is an old question, and the owner of it may have moved on a long time ago. Anyway:

The proposed solution will not work on ASP.NET MVC. And if you ask me, which you don't, I'll say this is not the cleanest solution to the problem:

Thomas Eyde
BTW, not that they are bad answers, but the IFrame one is almost EXACTLY the same as my solution, except it apparently DOES do it using Page_Load (so maybe my comment about using Render is misguided). The jQuery is polling, which means you have to have some mechanism to GET the state from the process that is running it, which isn't necessarily trivial either.
Michael Bray