I have been able to create an event with start date/time and end date/time. I want a situation where the event will not be seen until the start date/time reaches. Basically, I want to hide web page controls and redirect to another page if it is not yet time for the event to start. But if the start date/time reaches then the controls will be made visible for users.
For example if I create event and choose the date and time the event will start and end, and then save the date and time into database. Then on the web page, a message will read “Your Event will start in”. As soon as the set time/date starts the message will change to “Your Event will End in” and the web page where the event will hold will be on and controls will be seen else the controls will be hidden on that web page.
The Start/End Date and Time chosen by the user will be saved into the database where they will be used
Table Structure
UserId
|
EventName
|
Event_URL
|
StartDate
|
EndDate
|
1
|
2021 online games
|
http://localhost:60223/vote.aspx?id=dAFBBsw9
|
2021-10-02T10:00
|
2021-10-02T14:00
|
2
|
Junior School Quiz
|
http://localhost:60223/vote.aspx?id=4mxA25cp
|
2021-10-08T10:00
|
2021-10-08T16:00
|
The above table shows two events that will start and end at different dates and time. Each event is based on the UserId, and event will start and end based on the UserId. So if UserId = 1 sets start/end date and time to 2021-10-02T10:00 and 2021-10-02T14:00, then on the webpage controls will be visible when the start date/time reaches 2021-10-02T10:00 (Oct. 10, 2021 10:00AM) and also controls will hide when the date and time reaches 2021-10-02T14:00 (Oct. 10, 2021 2:00PM).
This is what I tried but it’s not working. It’s a bit complicating to understand
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div class="wrapper">
<asp:Label ID="LblstartDate" runat="server" Text=""></asp:Label>
<asp:Label ID="LblendDate" runat="server" Text=""></asp:Label>
<asp:Label ID="Labelurl" runat="server" Text="http://localhost:60223/vote.aspx?id=c8CzdW4J"></asp:Label>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div id="Div1" runat="server" class="alert alert-success">
<label id="Label3" runat="server">Your Event will start in</label>
<asp:Label ID="LabelTime" runat="server" Text="" />
<br />
</div>
<div id="Div2" runat="server" class="alert alert-success">
<label id="Label1" runat="server">Your Event will End in</label>
<asp:Timer ID="timer1" runat="server" OnTick="timer1_Tick" Interval="1000"></asp:Timer>
<asp:Label ID="lblTime" runat="server" />
<br />
</div>
<asp:Panel ID="controls" runat="server">
Name:
<asp:TextBox runat="server" ID="txtName" /><br />
Age:
<asp:TextBox runat="server" ID="txtAge" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
CODE
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
hidecontrols();
}
}
protected void timer1_Tick(object sender, EventArgs e)
{
hidecontrols();
}
private void hidecontrols()
{
SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM ElectionTable WHERE election_url = '" + Labelurl.Text + "'";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = cmd;
sda.Fill(ds, "detail");
if (ds.Tables[0].Rows.Count > 0)
{
DateTime startTime = Convert.ToDateTime(ds.Tables[0].Rows[0][4].ToString());
DateTime endTime = Convert.ToDateTime(ds.Tables[0].Rows[0][5].ToString());
lblTime.Text = CalculateT(startTime, endTime);
LabelTime.Text = CalculateT(startTime, endTime);
if (startTime >= endTime)
{
controls.Visible = false;
Div1.Visible = false;
Div2.Visible = true;
lblTime.Text = DateTime.Now.ToString();
}
else
{
controls.Visible = true;
Div1.Visible = true;
Div2.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;
}