How to disabling all control after Time Elapse
I am trying to create an event with timer, when the time elapses the event will be disabled including all its controls.
For example: I want a situation where an Administrator creates an event (like classroom examination) and sets duration for that examination; then other users will register with username and password. When the events starts, the page which has the examination questions will be opened and users can take part in the event (examination).
Then when the time elapse the users can no longer take part because all controls related to that particular examination will be disabled (examination will be closed), and result will be displayed.
Please how can I implement this?
Here is my test code
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EndDate", typeof(DateTime));
dt.Rows.Add("2021-08-07 00:30:34.943");
DateTime startDate = DateTime.Now;
DateTime endDate = Convert.ToDateTime(dt.Rows[0]["EndDate"].ToString());
LabelTime.Text = CalculateTime(startDate, endDate);
}
public string CalculateTime(DateTime startDate, DateTime endDate)
{
int days = 0; int hours = 0; int mins = 0; int secs = 0;
string finalTime = string.Empty;
if (endDate > startDate)
{
days = (endDate - startDate).Days;
hours = (endDate - startDate).Hours;
mins = (endDate - startDate).Minutes;
secs = (endDate - startDate).Seconds;
finalTime = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs);
}
return finalTime;
}
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div class="wrapper">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label ID="LabelTime" runat="server" Text=""/>
<asp:Timer ID="Timer" runat="server" Interval="1000"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>