Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Imports APNSoft.WebControls

Partial Public Class Rating_ProgressBar
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        'Set the total number of segments
        myRating.SegmentsInTotal = 10

        'Set segment values
        myRating.DataBind()
        For i As Integer = 0 To myRating.Segments.Count - 1
            myRating.Segments(i).value = (i + 1) * 10 'Each segment adds 10%
            myRating.Segments(i).title = myRating.Segments(i).value & "%" 'Set title
        Next i

        'Apply event-handling method
        AddHandler myRating.CallTheServer, AddressOf myRating_CallTheServer

    End Sub


    'The event-handling method
    Private Sub myRating_CallTheServer(ByVal sender As Object, ByVal e As APNSoft.WebControls.RatingEventArgs)
        'Pass the Percentage to client side via parameter
        e.Parameter = GetPercentageComplete().ToString()
    End Sub


    'Returns the current % complete for a task.
    Private Function GetPercentageComplete() As Integer
        'Declarations
        Dim _Result As Integer = 0
        Dim CompletionTime As Date
        Dim MilliSecondsRemain As Integer = 0
        Dim MilliSecondsInTotal As Integer = 10000 'The task takes 10 seconds

        'Get task status (the CompletionTime)
        Dim _CompletionTime As Object = Session("CompletionTime")
        If _CompletionTime Is Nothing Then
            'Create new task (the CompletionTime)
            CompletionTime = Date.Now.AddMilliseconds(MilliSecondsInTotal) 'The task takes 10 seconds
            Session("CompletionTime") = CompletionTime 'Save in session
        Else
            'Task is running (get existed CompletionTime from session)
            CompletionTime = CDate(_CompletionTime)
        End If

        'How many milliseconds remain until the task is completed?
        Dim span As TimeSpan = CompletionTime.Subtract(Date.Now)
        MilliSecondsRemain = CInt(Fix(Math.Round(span.TotalMilliseconds)))

        'Task is completed
        If MilliSecondsRemain <= 0 Then
            MilliSecondsRemain = 0
            Session("CompletionTime") = Nothing 'Clear session
        End If

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

        Return _Result
    End Function

End Class