How to use timer control in asp.net?

41,712

Solution 1

<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
    </asp:Timer> // For one minute
<asp:UpdatePanel ID="UpdatePanel1"
    runat="server">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick">
        </asp:AsyncPostBackTrigger>
    </Triggers>
</asp:UpdatePanel>

protected void Timer1_Tick(object sender, EventArgs e)
{
    // Label1.Text = DateTime.Now.Second.ToString();
}

Solution 2

One possible way would be to:

  • create an UpdatePanel where you would insert every question and the possible answers
  • in the UpdatePanel you can insert a UserControl that represents a question (for example this UserControl would contain a label with the question and some radio buttons or checkboxes with associated labels for the answers)
  • inside the UpdatePanel you could use the Timer control like in this example on Msdn http://msdn.microsoft.com/en-us/library/bb398865.aspx <asp:Timer id="Timer1" runat="server" Interval="120000" OnTick="Timer1_Tick"> </asp:Timer>
  • the function defined in the OnTick event of the Timer control will be fired on the server side after each time interval expires; there you would load the UserControl with the values for the next question and check if the test is finished or if the total time has elapsed (for example by summing the total elapsed time in a variable)

You could also display a countdown in the page if needed and use JavaScript to update it.

Share:
41,712
Admin
Author by

Admin

Updated on June 11, 2020

Comments

  • Admin
    Admin almost 4 years

    I am creating an online exam website and I want to set a 30 minute timer for 30 questions... Can anyone tell me how to use the timer control in asp.net or any other easy method?