Click here to Skip to main content
15,925,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 dropdownlist at a page 1 for hours ,2 for minutes,3 for AM/PM.

I want at run time that the current time should be shown in that in the sequence.Also,when the page is loaded it should take the current time and reflect on the drop down lists accordingly.How do I split the time in 3 parts and shown it in dropdownlist please ?

One more thing i have already filled dropdown by edit items.so duplicacy should not be occur at run time in dropdown.
Posted
Updated 3-Nov-11 2:42am
v4

C#
//add items on page load which can be from database as well
for (int i = 1; i <= 24; i++)
  { 
           hour.Items.Add(new ListItem(i.ToString(),i.ToString()));
  }
//set the current hour
hour.SelectedValue = DateTime.Now.Hour.ToString(); 

in the aspx page, following is the dropdown.
ASP.NET
<asp:dropdownlist id="hour" runat="server" xmlns:asp="#unknown"></asp:dropdownlist> 
 
Share this answer
 
C#
//Code To load hours and minutes in dropdown

int i;

                for (i = 0; i < 24; i++)
                {
                    if (i < 10)
                    {
                        ddlTime.Items.Add("0" + i.ToString());
                    }
                    else
                    {
                        ddlTime.Items.Add(i.ToString());
                    }
                }

                for (i = 0; i < 60; i++)
                {
                    if (i < 10)
                    {
                        ddlMinute.Items.Add("0" + i.ToString());
                    }
                    else
                    {
                        ddlMinute.Items.Add(i.ToString());
                    }
                }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900