Link to home
Start Free TrialLog in
Avatar of dabrat
dabrat

asked on

Drop Down List for Time and Date selection

Hello, I need to create a drop down list to let a user select the start time and date of an event.  I would like the current date displayed and then the time.  For example the drop down list will have selections ranging grom 7am to 11pm, with the current date in front of the time.

Can anyone help me achieve this?  I'm not sure how to code it.

Thanks
Avatar of Realmrat
Realmrat

Is this a windows application or a ASP.NET application?

 - Joe
Avatar of dabrat

ASKER

asp.net application
The code below uses System.Web.UI.WebControls.Calendar and a basic DropDownList - the dropdownlist can be made smarter as needed. Please tell me if this helps.

Try this:

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
   <script runat="server" >
 
      void Selection_Change(Object sender, EventArgs e)
      {

      // Get the selectedDate
      String selectedDate = Calendar1.SelectedDate.ToShortDateString();
      // Get the selected time
      string selectedTime = TimeList.SelectedItem.Value;
      //do processing with time and date
      // ...      
      }
   </script>
 
<body>
   <form runat="server">
      <h3> Date Time Picker </h3>
      Choose the date and time
      <br>
      <asp:Calendar id="Calendar1"
           ShowGridLines="True"
           ShowTitle="True"
           runat="server"/>
      <br>
      <table cellpadding="5">
         <tr>
            <td>
               Time:
            </td>
         </tr>
         <tr>
            <td>

               <asp:DropDownList id="TimeList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">
                  <asp:ListItem Selected="True" Value="7:00 AM"> 7:00 AM </asp:ListItem>
                  <asp:ListItem Value="8:00 AM"> 8:00 AM </asp:ListItem>
                  <asp:ListItem Value="9:00 AM"> 9:00 AM </asp:ListItem>
                  <asp:ListItem Value="10:00 AM"> 10:00 AM </asp:ListItem>
                  <asp:ListItem Value="11:00 AM"> 11:00 AM </asp:ListItem>
                  <asp:ListItem Value="12:00 AM"> 12:00 AM </asp:ListItem>
                  <asp:ListItem Value="1:00 PM"> 1:00 PM </asp:ListItem>
               </asp:DropDownList>

            </td>
        </tr>
     </form>
</body>
</html>
Avatar of dabrat

ASKER

this does not add the currnt date to the drop down list.  I need the drop down list to display current date then the time.

example ( each line would be a selection in the drop down list)

10/14/2004 7:00am
10/14/2004 7:30am

and so on
Add this function to the Load event of the dropdown list:

private void TimeList_Load(object sender, System.EventArgs e)
{
     //TimeList is the drop down list
      TimeList.Items.Clear;
      //populate array of times - put in all relevant values
      string[] timeArrList = {" 7:00 AM"," 7:30 AM "," 8:00 AM "};
      string currDate = Calendar1.SelectedDate.ToShortDateString();
      for(int i=0;i<timeArrList.Length;i++)
      {
      timeArrList[i] = currDate + timeArrList[i];
      TimeList.Items.Add(timeArrList[i]
      }
}

This will show items in the drop down the way you want. Change the format of the date if you need to show it differently.
ASKER CERTIFIED SOLUTION
Avatar of aacool
aacool

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dabrat

ASKER

okay the code you gave me works if I do not use my stored procedure but if I use it i get the following error:

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated
Avatar of dabrat

ASKER

thank you so much!!!