troubleshooting Question

Datetime values being overwritten

Avatar of mccs-webmaster
mccs-webmaster asked on
4 Comments1 Solution326 ViewsLast Modified:
Hello. First, I apologize. I am new to C# and ASP.NET (but not new to programming), and I'm having a little bit of trouble with the transition. My question for the day seems to stem off of the basis that I'm not quiet used to working with strong-type languages and I don't quite understand the OOP nature of ASP.NET.

I think my code will probably explain the problem I am having more than anything, but basically, I have three class variables: tmpDate, tmpTime, and newDateTime. I'll probably be getting rid of these after I figure out the solution to what I am trying to do...

I am trying to build my own date and time selectors. If you click on the calendar image, the calendar is shown, and the user can choose a date. If you click on the clock image, a dropdownlist is shown with selectable times.

When a date is chosen, I want to update the class variable tmpDate. When a time is chosen, I want to update the class variable tmpTime. In the end, I take values from tmpDate and tmpTime and create a new DateTime value and stuff it in the textbox.

This isn't happening. If I choose a date, the date variable is updated and the time variable is overwritten. Likewise, if I choose a time, the date variable is overwritten to 1/1/0001. What is going on here? Why aren't the class variables keeping their values?

That's the whole question, really. Why aren't the class variables maintaining their values? Why are they being overwritten? Please see the code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DateTime.aspx.cs" Inherits="BeginningASPNET.PageDateTime" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <form id="dateTimeTest" runat="server">
        <asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
        <asp:ImageButton ID="ibDate" runat="server" AlternateText="Choose Date" 
            ImageUrl="i/date_ico.jpg" onclick="ibDate_Click" />
        <asp:ImageButton ID="ibTime" runat="server" AlternateText="Choose Time" 
            ImageUrl="i/time_ico.jpg" onclick="ibTime_Click" />
        <br />
        <asp:Calendar ID="calStartDate" runat="server" 
        <asp:DropDownList ID="ddlStartTime" runat="server" 
            onselectedindexchanged="ddlStartTime_SelectedIndexChanged" AutoPostBack="true">
        <asp:Label ID="lblResults" runat="server" />
C# Code
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace BeginningASPNET
    public partial class PageDateTime : System.Web.UI.Page
        public DateTime tmpDate;
        public DateTime tmpTime;
        public DateTime newDateTime;
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                calStartDate.Visible = false;
                ddlStartTime.Visible = false;
        protected void PopulateTimeDDL()
            ArrayList arrayTime = new ArrayList();
            //config variables
            int minuteInterval = 30;
            int hourEarliest = 10;
            int hourLatest = 19;
            //ddl list item variables
            string strHour;
            string strMinute;
            //build the hour value
            for (int hour = hourEarliest; hour <= hourLatest; hour++)
                if (hour < 10)
                    strHour = "0" + hour.ToString();
                    strHour = hour.ToString();
                //build the minute value
                for (int minute = 0; minute < 60; minute = minute + minuteInterval)
                    if (minute < 10)
                        strMinute = "0" + minute.ToString();
                        strMinute = minute.ToString();
                    //add the list item
                    string resultTime = strHour + ":" + strMinute;
            ddlStartTime.DataSource = arrayTime;
        protected void calStartDate_SelectionChanged(object sender, EventArgs e)
            this.tmpDate = calStartDate.SelectedDate;
        protected void ddlStartTime_SelectedIndexChanged(object sender, EventArgs e)
            this.tmpTime = Convert.ToDateTime(ddlStartTime.SelectedValue);
        protected void ibDate_Click(object sender, ImageClickEventArgs e)
            calStartDate.Visible = true;
            ddlStartTime.Visible = false;
        protected void ibTime_Click(object sender, ImageClickEventArgs e)
            calStartDate.Visible = false;
            ddlStartTime.Visible = true;
        protected void UpdateStartDateTime()
            DateTime date = this.tmpDate;
            DateTime time = this.tmpTime;
            this.newDateTime = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, 0);
            //txtStartDate.Text = this.newDateTime.ToString();
            //lblResults.Text = "<p>Date: " + date.ToShortDateString();
            //lblResults.Text += "<br />Time: " + time.ToShortTimeString();
            //lblResults.Text += "<br />Full: " + this.newDateTime.ToString() + "</p>";
            //lblResults.Text += "<p>Year: " + date.Year + " Month: " + date.Month + "Day: " + date.Day + "</p>";
            //lblResults.Text += "<p>Hour: " + time.Hour + " Min: " + time.Minute + "</p>"; 
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros