Question

I have two Drop down list.

First drop down is list of months and second is for days.

I want when I select month from first drop down, items of second dropdown change according to selection of first dropdown.

For Example- if I select January from first dropdown, second drop down list become 1 to 31. On selction of february, second drop down list become 1 to 29.

I am trying with this code but not working-

protected void Page_Load(object sender, EventArgs e)
    {
        getDays(dropdownMonth.SelectedValue);

    }

public void getDays(string selectedMonth)
    {
        int i = 0;
         dropdownDays.Items.Clear();
        switch (selectedMonth)
        {
            case "January":
                i = 31;
                break;
            case "February":
                i = 29;
                break;
            case "March":
                i = 31;
                break;
            case "April":
                i = 30;
                break;
            case "May":
                i = 31;
                break;
            case "June":
                i = 30;
                break;
            case "July":
                i = 31;
                break;
            case "August":
                i = 31;
                break;
            case "September":
                i = 30;
                break;
            case "October":
                i = 31;
                break;
            case "November":
                i = 30;
                break;
            case "December":
                i = 31;
                break;
        }

        for (int j = 1; j <= i; j++)
        {
            dropdownDays.Items.Add(j.ToString());
        }
        int Month = DateTime.ParseExact(Convert.ToString(dropdownMonth.SelectedValue), "MMMM", CultureInfo.CurrentCulture).Month;
        int day = Convert.ToInt32(dropdownDays.SelectedValue);
        int year = DateTime.Now.Year;
        DateTime date = new DateTime(year, Month, day);
        lblEndDateValue.Text = String.Format("{0:dd MMMM}", date.AddDays(-1));

if i remove dropdownmonth.items.clear(); from my code then it works but then it adds new days list to previous list on this dropdown.

Was it helpful?

Solution

Use the Selected index changed event for first dropdownlist and set autopostback=true for first dropdown

   protected void dropdownMonth_SelectedIndexChanged(object sender, EventArgs e)
   {
       getDays(dropdownMonth.SelectedValue);
   }

set AutoPostBack="true" for dropdownDays also,

  protected void dropdownDays_SelectedIndexChanged(object sender, EventArgs e)
    {
        LabelDays.Text = dropdownDays.SelectedValue;
    }

OTHER TIPS

Try this..You must bind your code inside !ispostback..Otherwise the on each postback the value get reset..

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostback)
   {
       //getDays(dropdownMonth.SelectedValue);
   }
}

You can call your function in selectedidexchanged event of month dropdown and set Autopostback=true for the dropdown..I don't think you must call it on pageload..

 protected void dropdownMonth_SelectedIndexChanged(object sender, EventArgs e)
   {
       getDays(dropdownMonth.SelectedValue);
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top