Click here to Skip to main content
15,897,187 members
Articles / Web Development / ASP.NET

Web Form with Progress Bar of Web Service Execution

Rate me:
Please Sign up or sign in to vote.
2.00/5 (4 votes)
6 Aug 2008CPOL1 min read 44.1K   1K   28  
Show developers how to manage state of long running web service and then use this state in Ajax enabled web form
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;

public partial class _Default : System.Web.UI.Page
{
    MyService.Test ts = new MyService.Test();
    MyService.ProgressValues pw = null;
    int steps = 5;
    int values = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)   // Page Loaded first time
        {
            CleanPage();                // Drop all values of progress bar to zero   
        }
    }
    private void CleanPage()
    {
        L_P1.Text = "";
        L_P2.Text = "";
        L_Status.Text = "";
        Div3.Visible = false;
        Div4.Visible = false;
        Div1.Style.Add(HtmlTextWriterStyle.Width, "0");
        Div2.Style.Add(HtmlTextWriterStyle.Width, "0");
        GridView1.DataSource = null;
        GridView1.DataBind();
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        IAsyncResult my_ar = (IAsyncResult)Session["my_ar"];            // Get IAsyncResult from Session
        pw = ts.GetProcessValues(Session["WebServiceUID"].ToString());  // Get ProgressValues from web service
        if (my_ar.IsCompleted)                                          // Async execution of service done
        {
            DataTable tbl = ts.EndGo(my_ar);                    // Get DataTable from web service
            if (tbl != null)                                    // Completed good
            {
                GridView1.DataSource = tbl;                     // Bind Grid to datasou
                GridView1.DataBind();                           // Bind Grid to data
                L_Status.Text = "Completed!";
                Timer1.Enabled = false;                         // Stop timer ticks
            }
            else                                                // Aborted by user
            {
                CleanPage();
                L_Status.Text = "Aborted by user!";
                Timer1.Enabled = false;                         // Stop timer ticks
                return;
            }
        }
        // Dispaley label text
        L_P1.Text = pw.step.ToString()+"/"+steps.ToString();
        L_P2.Text = pw.value.ToString()+"/"+values.ToString();
        // Refresh progress bar length
        Div1.Style.Value = "BACKGROUND-IMAGE: url(Images/SubBG2.jpg); width:" + (pw.step * (200/steps)).ToString() + "px; height:20px;";
        Div2.Style.Value = "BACKGROUND-IMAGE: url(Images/SubBG2.jpg); width:" + (pw.value * (200/values)).ToString() + "px; height:20px;";
    }
    protected void B_Start_Click(object sender, EventArgs e)
    {
        CleanPage();                            // Drop all values of progress bar to zero
        string UID = GetRandomName();           // Get random name for wev service instance
        Session["WebServiceUID"] = UID;         // Remember UID of a service in Session for later use
        IAsyncResult my_ar;                     // AsyncResult for async execution of service
        my_ar = ts.BeginGo(UID, steps, values, null, null);    // Start async execution of web service
        Session["my_ar"] = my_ar;               // Remember AsyncResult in Session for later use
        Div3.Visible = true;
        Div4.Visible = true;
        L_Status.Text = "In progress!";
        Timer1.Enabled = true;                  // Enable timer for partial page refreshes
    }
    protected void B_Stop_Click(object sender, EventArgs e)
    {
        if (Session["WebServiceUID"] != null)
            ts.Abort(Session["WebServiceUID"].ToString());
    }
    public string GetRandomName()
    {
        byte[] by = new byte[5];
        System.Random r = new Random();
        r.NextBytes(by);
        string o = "";
        foreach (byte b in by)
            o += string.Format("{0,0:X2}", (byte)b);
        return o.ToUpper();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions