dropdown date

hi,
  i've set autopostback of my 3 dropdownlist(year,month,day) to true.
  this code is working but i want that whenever month and year i choose, like feb 2005 the days to be displayed should only 1-28 only , if feb 2004 the days should be 1-29 or may 2005 should be 1-31. what's happening with this code is that it displayed in my dropdownlist september ,1-30 ,2005 but when i choose may, 2005 the days only display 1-30 only which should be 1-31.
Please help me what's missing.


if (!Page.IsPostBack)
{   
ArrayList MyYears = new ArrayList();
int yy = DateTime.Now.Year;
       
MyYears.Add(yy -1);
MyYears.Add(yy);
    
dropdownyear.DataSource = MyYears;
dropdownyear.DataBind();
dropdownyear.SelectedIndex = 1;
  
dropdownmonth.SelectedValue = DateTime.Now.Month.ToString();

int year = Convert.ToInt32(dropdownyear.SelectedValue);
int month = Convert.ToInt32(dropdownmonth.SelectedValue);
int Day = DateTime.Now.Day;
    
dropdownday.DataSource = GetTotalDays(month, year);
dropdownday.DataBind();
dropdownday.SelectedIndex = Day - 1;
}


 



Answer this question

dropdown date

  • Luca Bolognese

    thank you very much.
  • A D SRIKANTH

    i see several problems with this code. First of them being that you are trying to do something which is best done using client-side JavaScript on the server side.

    I'd suggest that on the client-side you write a javascript that determines that the year and month and auto-generates the days for you.

    Secondly you have placed this code block in !Page.PostBack which means that this code will only execute when the page renders the first time. So when your dropdown lists auto-postback this code is never called hence you get incorrect dates. The best way to diagnose such situations is to place a breakpoint in the debugger on the line that calls GetTotalDays. Now run the page in debug mode. YOu will quickly find out that the debugger never breaks on postbacks since the code is never called except when the page is first rendered. To fix this problem remove the if block.


    Regards,
    Saurabh Nandu
    www.MasterCSharp.com
    www.AksTech.com

  • dropdown date