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

using APNSoft.WebControls;

public partial class Rating_ProgressBar : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //Set the total number of segments
        myRating.SegmentsInTotal = 10;

        //Set segment values
        myRating.DataBind();
        for (int i = 0; i < myRating.Segments.Count; i++)
        {
            myRating.Segments[i].value = (i + 1) * 10; //Each segment adds 10%
            myRating.Segments[i].title = myRating.Segments[i].value + "%"; //Set title
        }

        //Apply event-handling method
        myRating.CallTheServer += new APNSoft.WebControls.RatingEventHandler(myRating_CallTheServer);

    }


    //The event-handling method
    void myRating_CallTheServer(object sender, APNSoft.WebControls.RatingEventArgs e)
    {
        //Pass the Percentage to client side via parameter
        e.Parameter = GetPercentageComplete().ToString();
    }


    //Returns the current % complete for a task.
    private int GetPercentageComplete()
    {
        //Declarations
        int _Result = 0;
        DateTime CompletionTime;
        int MilliSecondsRemain = 0;
        int MilliSecondsInTotal = 10000; //The task takes 10 seconds

        //Get task status (the CompletionTime)
        object _CompletionTime = Session["CompletionTime"];
        if (_CompletionTime == null)
        {
            //Create new task (the CompletionTime)
            CompletionTime = DateTime.Now.AddMilliseconds(MilliSecondsInTotal); //The task takes 10 seconds
            Session["CompletionTime"] = CompletionTime; //Save in session
        }
        else
        {
            //Task is running (get existed CompletionTime from session)
            CompletionTime = (DateTime)_CompletionTime;
        }

        //How many milliseconds remain until the task is completed?
        TimeSpan span = CompletionTime.Subtract(DateTime.Now);
        MilliSecondsRemain = (int)Math.Round(span.TotalMilliseconds);

        //Task is completed
        if (MilliSecondsRemain <= 0)
        {
            MilliSecondsRemain = 0;
            Session["CompletionTime"] = null;//Clear session
        }

        //Calculate the % complete for the task
        _Result = ((MilliSecondsInTotal - MilliSecondsRemain) * 100) / MilliSecondsInTotal;

        return _Result;
    }

}