首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据当前月份的页面加载情况选择下拉列表中的一个月?

如何根据当前月份的页面加载情况选择下拉列表中的一个月?
EN

Stack Overflow用户
提问于 2015-07-01 05:40:20
回答 4查看 3.5K关注 0票数 0

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

我已经尝试了下面的代码,但是我发现了一个错误:

“不能在下拉列表中选择多项”。

我不知道这意味着什么,因为我没有选择其他项目。

(我的月份下拉列表目前有从1月到12月的列表项,索引为0-11)

代码语言:javascript
复制
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;
            }

        }

为了解决这个问题,我需要在代码中修改什么?或者,是否有其他解决方案可以用于自动选择当前月份?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-07-01 05:46:53

你可以这样做

代码语言:javascript
复制
 MonthDropDownList.SelectedIndex = month - 1;
票数 2
EN

Stack Overflow用户

发布于 2015-07-01 05:49:13

如果将下拉值生成为月份号,则在page_load事件中

代码语言:javascript
复制
MonthDropDownList.SelectedValue = DateTime.Now.Month.ToString();

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

代码语言:javascript
复制
MonthDropDownList.SelectedIndex= DateTime.Now.Month -1;

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

票数 1
EN

Stack Overflow用户

发布于 2015-07-01 05:56:50

避免代码中的for循环。

您也可以像这样使用c#

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

代码在中的应用

代码语言:javascript
复制
    <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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31153311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档