how to change second drop down item on selection f

2019-09-20 16:45发布

问题:

I am using panel.

I have two drop down list. one is for month and another is for days.

Question 1

I want whenever I select month from first drop down, no of days in second drop down become according to selection of month.

For Example- if I am selecting "January" then second drop down list become from 1 to 31. If I am selecting "Febuary" second drop down list become 1 to 29 and so on..

Question2

I am selecting startdate from dropdowns and I am showing last date in label.

For example- If I am selecting "January" from first dropdown. Date "1" from second dropdown.
Then Laabel1.text become last date i.e. 31 december.

Screenshot of my panel is-

I am trying this but it's not working.

public void getDaysOnMonthSelection(string Month)
    {
        int i=0;
        switch (Month)
        {
            case "January":
                i = 31;
                break;
            case "Febuary":
                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;
        }
        dropdownDays.Items.Clear();
        for (int j = 1; j <= i; j++)
        {
            dropdownDays.Items.Add(j.ToString());
        }
        lblEndDateValue.Text = dropdownDays.Text + "  " + dropdownMonth.Text;

    }

回答1:

For first answer you have to remove one line of your code-

Remove this line-

dropdownDays.Items.Clear();