views:

215

answers:

2

Hey All

Developing is more of a hobby at the moment; so apologies if this is a basic question, although after a couple of hours searching the web I can't seem to find an answer. I am currenttly building a support tool for my team which runs a number of SQL queries & BCP processes and returns the results. Some of these actions take quite a while and I would like to update the web UI with status messages. Previously I have been using the Label1.Text = text;

However now I would like to update the Label1.text multiple times while my method is running. Try as I might though it only displays the last update in the method.

I have coded a small test to try get to the bottom of this: -

Web Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Template._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>Untitled Page</title>


</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Label ID="progress" runat="server" Text=""></asp:Label><br />
    <asp:Image ID="Image1" runat="server" ImageUrl="~/Image/loader.gif" 
        Visible="False" />
</div>
</form>
</body>
</html>

C# CodeBehind

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Template
{
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uiUpdate("OUT", 1);           
        wait();
        uiUpdate("In", 0);
    }

    protected void uiUpdate(string text, Int32 image)
    {                      
        if (image == 1)
        {
            Image1.Visible = true;                                             
        }
        else
        {
            Image1.Visible = false;
        }
        progress.Text = text;

    }

    protected void wait()
    {
        DateTime wait;
        wait = DateTime.Now + TimeSpan.FromSeconds(10);
        while (wait > DateTime.Now)
        {
        }
    }

Is there a way I can get the Label1 & Image to update as the Page_load is running?

Or am I looking at this the totally wrong way (no doubt!)

Thanks

+2  A: 

HTML as a full page request is not in the least bit granular:

  • your browser sends a request (of all the form variables, cookies, etc etc) to the server
  • the server does a bit of processing and sends a response (typically html)
  • your browser processes the response and renders the display

All your updates are happening in the middle bullet, so no: there is no way to make the browser update: your code is executing on the server, with no dialog with the client

If you need this type of behaviour, you would have to look at AJAX tools - at each step doing "just enough" work to get to the next milestone and update the caption... with regular ASP.NET UpdatePanel may be of interest; for MVC or other (non-ASP.NET) setups, jQuery would be your friend. Unfortunately, this isn't a trivial topic.

Marc Gravell
Much appreciated!
Scottb
A: 

If I understand your question correct you can't do this in regular asp.net, you need to use ajax, and make an ajax method in which you then update the text of the label on runtime. You can use the ajax MS scriptmanager to do this. If it is not for high performance you can use an update panel otherwise you should just use the scriptmanager by yourself.

Dofs