Monday, June 28, 2010

How to show the progress of long running operation - Simple Example

Demo:
This article demonstrates how to show the progress of a long running operation using simple label text. But you can add a spning progress indicator and make it much fancy.

Screenshots:


Markup:
<%@ Page Language="C#" 
    AutoEventWireup="true" 
    CodeBehind="Test.aspx.cs" 
    Inherits="ActiveTest.Test"  %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        .Hide { display:none; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Button runat="server" ID="btnDoTask" Text="Do Task" OnClick="Button1_Click" />
                <asp:Label runat="server" ID="lblMessage" />
                <asp:Button runat="server" ID="btnHidden" UseSubmitBehavior="false" CssClass="Hide" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
 Code behind:
public partial class TestPage
{
    private string script = @"setTimeout(""__doPostBack('{0}','')"", 5000);";
    public bool Processing
    {
        get { return (bool)(Session["Processing"] ?? false); }
        set { Session["Processing"] = value; }
    }
    public bool Completed
    {
        get
        {
            return (bool)(Session["Completed"] ??
                (Session["Completed"] = true));
        }
        set { Session["Completed"] = value; }
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (this.Processing)
        {
            this.lblMessage.Text = "Processing... Please wait...";
            ScriptManager.RegisterStartupScript(thisthis.GetType(),
                this.GetType().Name, string.Format(script, this.btnHidden.ClientID),
                true);
        }
        else
        {
            if (!this.Completed)
            {
                lblMessage.Text = "Processing is complete";
                this.Completed = true;
            }
            else
                lblMessage.Text = string.Empty;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(new ThreadStart(SomeLongOperation));
        thread.Start();
        this.lblMessage.Text = "Processing... Please wait...";
        this.Processing = true;
        ScriptManager.RegisterStartupScript(thisthis.GetType(),
            this.GetType().Name, string.Format(script, this.btnHidden.ClientID), true);
    }
    void SomeLongOperation()
    {
        this.Completed = false;
        for (int i = 0; i < 20; i++)
            Thread.Sleep(1000);
        this.Processing = false;
    }
} 

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...