Run events based on start and end time using C# and VB.Net in ASP.Net

RichardSa
 
on Aug 19, 2021 12:39 AM
Sample_745094.zip
497 Views

I really don’t know how to put this. But i really need help on this:

Recently, I have been doing serious thinking on how to achieve this; I have not done it before and don't even know how to do it.

I want to create a situation where Admin creates an examination with start dates/time and end dates/time, as well as create questions and options too, where users will choose from and submit their answers. After this, the Admin will launch the examination BUT I DON'T WANT THE

EXAMINATION TO BE ACTIVE AND RUNNING UNTIL WHEN THE SET DATE AND TIME STARTS. AND ALSO WHEN THE END DATE AND TIME STOPS, THE EXAMINATION SHOULD BE CLOSED.

Basically I want to achieve the description typed in capital characters.

I really need your help. Please help me give an insight on how I can create this.

Thank you very much

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 19, 2021 05:54 AM

Hi RichardSa,

Use Ajax timer control to check the time.

Refer below example.

HTML

<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Timer ID="timer1" runat="server" OnTick="timer1_Tick" Interval="1000"></asp:Timer>
        <asp:Label ID="lblTime" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

Code

C#

protected void timer1_Tick(object sender, EventArgs e)
{
    DateTime startTime = new DateTime(2021, 08, 19, 16, 50, 00);
    DateTime endTime = new DateTime(2021, 08, 19, 18, 05, 00);
    if (DateTime.Now > startTime && DateTime.Now < endTime)
    {
        // Do your task.
        lblTime.Text = DateTime.Now.ToString();
    }
}

VB.Net

Protected Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
    Dim startTime As DateTime = New DateTime(2021, 08, 19, 16, 50, 00)
    Dim endTime As DateTime = New DateTime(2021, 08, 19, 18, 05, 00)

    If DateTime.Now > startTime AndAlso DateTime.Now < endTime Then
        lblTime.Text = DateTime.Now.ToString()
    End If
End Sub