如何在页面加载时根据当前月份在下拉列表中 select 一个月?

How to select a month in drop down list according current month upon page load?

我想编写这样的代码,当我 运行 我的程序时,月份下拉列表会根据 date.time.now 自动 selected。我加载页面的月份。

我已经尝试了下面的代码,但我收到一条错误消息:

" Cannot have multiple items selected in a Drop Down List".

我不确定这意味着什么,因为我没有其他物品 selected。

(我的月份下拉列表当前包含从一月到十二月的列表项,索引为 0-11)

int month = DateTime.Now.Month;

        for (int i = 0; i < MonthDropDownList.Items.Count; i++)
        {
            if (month == 1) //Jan
            {
                MonthDropDownList.Items[0].Selected = true;
            }
            else if (month == 2) //Feb
            {
                MonthDropDownList.Items[1].Selected = true;
            }
            else if (month == 3) //March
            {
                MonthDropDownList.Items[2].Selected = true;
            }
            else if (month == 4) //April
            {
                MonthDropDownList.Items[3].Selected = true;
            }
            else if (month == 5) //May
            {
                MonthDropDownList.Items[4].Selected = true;
            }
            else if (month == 6) //June
            {
                MonthDropDownList.Items[5].Selected = true;

            }
            else if (month == 7) //July
            {
                MonthDropDownList.Items[6].Selected = true;
            }
            else if (month == 8) //Aug
            {
                MonthDropDownList.Items[7].Selected = true;
            }
            else if (month == 9) //Sept
            {
                MonthDropDownList.Items[8].Selected = true;
            }
            else if (month == 10) //Oct
            {
                MonthDropDownList.Items[9].Selected = true;
            }
            else if (month == 11) //Nov
            {
                MonthDropDownList.Items[10].Selected = true;
            }
            else if (month == 12) //Dec
            {
                MonthDropDownList.Items[11].Selected = true;
            }

        }

我必须更改代码中的哪些内容才能解决此问题?或者我可以使用另一种解决方案来自动 select 当前月份吗?[=​​13=]

你不需要for语句,条件(if else)就够了

你可以简单地这样做

 MonthDropDownList.SelectedIndex = month - 1;

在您的 page_load 活动中,如果您将下拉列表值构建为月份数字

MonthDropDownList.SelectedValue = DateTime.Now.Month.ToString();

或者您可以像在代码中那样使用

MonthDropDownList.SelectedIndex= DateTime.Now.Month -1;

我总是使用 SelectedValue,因为我公司的索引不可靠:)

避免代码中的 for 循环。

你也可以这样使用 c#

MonthDropDownList.SelectedValue = DateTime.Now.Month.ToString();

代码隐藏

    <asp:ListItem Value="1">Jan</asp:ListItem>
    <asp:ListItem Value="2">Feb</asp:ListItem>
    <asp:ListItem Value="3">March</asp:ListItem>
    <asp:ListItem Value="4">Apr</asp:ListItem>
    <asp:ListItem Value="5">May</asp:ListItem>
    <asp:ListItem Value="6">Jun</asp:ListItem>
    <asp:ListItem Value="7">July</asp:ListItem>
    <asp:ListItem Value="8">Aug</asp:ListItem>
    <asp:ListItem Value="9">Sep</asp:ListItem>
    <asp:ListItem Value="10">Oct</asp:ListItem>
    <asp:ListItem Value="11">Nov</asp:ListItem>
    <asp:ListItem Value="12">Dec</asp:ListItem>