Solved

Drop Down List for Time and Date selection

Posted on 2004-10-13
8
700 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:dabrat
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:Realmrat
ID: 12305683
Is this a windows application or a ASP.NET application?

 - Joe
0
 
LVL 1

Author Comment

by:dabrat
ID: 12307215
asp.net application
0
 
LVL 3

Expert Comment

by:aacool
ID: 12310902
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>
0
 
LVL 1

Author Comment

by:dabrat
ID: 12314240
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:aacool
ID: 12321652
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.
0
 
LVL 3

Accepted Solution

by:
aacool earned 50 total points
ID: 12321692
Small coding error  - missing bracket and ;

private void TimeList_Load(object sender, System.EventArgs e)
{
   TimeList.Items.Clear;
   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]);
   }
}
0
 
LVL 1

Author Comment

by:dabrat
ID: 12323362
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
0
 
LVL 1

Author Comment

by:dabrat
ID: 12323997
thank you so much!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now