Display Event Countdown Timer using C# in ASP.Net

RichardSa
 
on Sep 30, 2021 11:37 PM
1085 Views

I have a timer that shows when an event is scheduled to start and end, but when the set time for the event has been reached, the timer disappears.

I tried to continue to show timer so that it will display the remaining time for the event to end but it did not work

The event ended message shows. The timer during the event that did not show.

The Timer shows before the event, but when the event starts, the timer is not showing

Code:

private void hidecontrols()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("UserId");
    dt.Columns.Add("EventName");
    dt.Columns.Add("Event_URL");
    dt.Columns.Add("StartDate");
    dt.Columns.Add("EndDate");
    dt.Rows.Add(1, "Board of Directors", "http://localhost:60223/vote.aspx?id=dAFBBsw9", "2021-10-16 10:00", "2021-10-16 16:00");
 
    DataSet ds = new DataSet();
    ds.Tables.Add(dt);
    if (ds.Tables[0].Rows.Count > 0)
    {
        DateTime startTime = Convert.ToDateTime(ds.Tables[0].Rows[0][3].ToString());
        DateTime endTime = Convert.ToDateTime(ds.Tables[0].Rows[0][4].ToString());
        if (startTime > DateTime.Now)
        {
            controls.Visible = false;
            LabelTime.Text = CalculateT(DateTime.Now, startTime);
            Div1.Visible = true;
            Div2.Visible = false;
        }
        else if (endTime > DateTime.Now)
        {
            controls.Visible = true;
            lblTime.Text = CalculateT(endTime, DateTime.Now);
            Div1.Visible = false;
            Div2.Visible = true;
            timer1.Enabled = true; //timer1 has been enabled here to display when the event starts
        }
        else
        {
            controls.Visible = false;
            Div1.Visible = true;
            Div2.Visible = false;
            LabelTime.Text = "Event Has Ended";
            Label3.Visible = false;
        }
    }
}
 
public string CalculateT(DateTime startTime, DateTime endTime)
{
    int days = 0; int hours = 0; int mins = 0; int secs = 0;
    string finalTime = string.Empty;
    if (endTime > startTime)
    {
        days = (endTime - startTime).Days;
        hours = (endTime - startTime).Hours;
        mins = (endTime - startTime).Minutes;
        secs = (endTime - startTime).Seconds;
        finalTime = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs);
    }
    return finalTime;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 05, 2021 08:05 AM

Refer below code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        hidecontrols();
    }
}

protected void timer1_Tick(object sender, EventArgs e)
{
    hidecontrols();
}

static int interval = 0;
private void hidecontrols()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("UserId");
    dt.Columns.Add("EventName");
    dt.Columns.Add("Event_URL");
    dt.Columns.Add("StartDate");
    dt.Columns.Add("EndDate");
    dt.Rows.Add(1, "Board of Directors", "http://localhost:60223/vote.aspx?id=dAFBBsw9", "2021-10-16 10:00", "2021-10-16 16:00");

    DataSet ds = new DataSet();
    ds.Tables.Add(dt);
    if (ds.Tables[0].Rows.Count > 0)
    {
        DateTime startTime = Convert.ToDateTime(ds.Tables[0].Rows[0][3].ToString());
        DateTime endTime = Convert.ToDateTime(ds.Tables[0].Rows[0][4].ToString());
        if (startTime < endTime && endTime <= DateTime.Now)
        {
            LblstartDate.Visible = false;
            LblendDate.Visible = false;
            namelbl.Visible = false;
            Label2.Text = ds.Tables[0].Rows[0][1].ToString();
            Labelurl.Text = ds.Tables[0].Rows[0][2].ToString();
            timer1.Enabled = false;
            controls.Visible = false;
            Label1.InnerHtml = "";
            lblTime.Text = "This Event has Ended";
            Div1.Visible = false;
            Div2.Visible = true;
        }
        else if (startTime < endTime && endTime > DateTime.Now)
        {
            controls.Visible = true;
            lblTime.Text = CalculateT(DateTime.Now, endTime);
            Div1.Visible = false;
            Div2.Visible = true;
        }
        else if (startTime < DateTime.Now)
        {
            controls.Visible = false;
            Div1.Visible = true;
            Div2.Visible = false;
            interval++;
            startTime = startTime.AddSeconds(interval);
            LabelTime.Text = CalculateT(startTime, endTime);
        }
        else if (startTime > DateTime.Now)
        {
            controls.Visible = false;
            Div1.Visible = true;
            Div2.Visible = false;
            interval++;
            startTime = startTime.AddSeconds(interval);
            LabelTime.Text = CalculateT(DateTime.Now, endTime);
        }
    }
}

public string CalculateT(DateTime startTime, DateTime endTime)
{
    int days = 0; int hours = 0; int mins = 0; int secs = 0;
    string finalTime = string.Empty;
    if (endTime > startTime)
    {
        days = (endTime - startTime).Days;
        hours = (endTime - startTime).Hours;
        mins = (endTime - startTime).Minutes;
        secs = (endTime - startTime).Seconds;
        finalTime = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs);
    }
    return finalTime;
}