Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,


C#
protected void bindtimedropdown()
      {
          string ktr, mtr;
          for (int k = 0; k < 24; k++)
          {
              for (int m = 0; m < 60; m=m+30)
              {
                  if (k < 10)
                  {
                      ktr = "0" + k.ToString();
                  }
                  else
                  {
                      ktr = k.ToString();
                  }
                  if (m < 10)
                  {
                      mtr = "0" + m.ToString();
                  }
                  else
                  {
                      mtr = m.ToString();
                  }
                  DropDownList1.Items.Add(new ListItem(ktr + ":" + mtr));
              }
          }
      }


This is the code which I am using to bind the dropdown to select the time. Now I want user can not select the future time for the today date. How it is possible? Can any one explain me??

Thanks, in advance
Posted
Comments
ZurdoDev 2-Apr-13 7:13am    
Either don't add future dates to your dropdown or when they do pick one, check the value.
kishore sharma 2-Apr-13 8:28am    
Hi make it clear what all the possibilities use may select.
kishore sharma 3-Apr-13 0:42am    
HI can you explain the UI of this page ,is it having datepicker or all are dropdownlist controls for date,month,year,Hour,mi,sec????

1 solution

try this code
C#
protected void bindtimedropdown()
    {
        string ktr, mtr;
        for (int k = 0; k < DateTime.Now.Hour; k++)
        {
            for (int m = 0; m < DateTime.Now.Minute; m = m + 30)
            {
                if (k < 10)
                {
                    ktr = "0" + k.ToString();
                }
                else
                {
                    ktr = k.ToString();
                }
                if (m < 10)
                {
                    mtr = "0" + m.ToString();
                }
                else
                {
                    mtr = m.ToString();
                }
                DropDownList1.Items.Add(new ListItem(ktr + ":" + mtr));
            }
        }
    }
 
Share this answer
 
Comments
kishore sharma 2-Apr-13 8:24am    
by this it will also restrict user to select past date time .
suppose he want to select yestrday 7pm and current time is 5pm then he can't select that time as well.
Pallavi Waikar 2-Apr-13 9:21am    
then he has to provide one datetimepicker.. and based on it dropdownlist is filled..if date is privious then no need to check hourse or minute and if date==datetime.now() then he has to use above code and if date is greater than today date then no data is inserted in dropdownlist..it is done by using if else statement

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