为 ASP.NET 编写月份和年份下拉列表的最佳方法是什么? [英] What is the best way to code up a Month and Year drop down list for ASP.NET?

查看:21
本文介绍了为 ASP.NET 编写月份和年份下拉列表的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内部应用程序,我需要一个包含两个日期类型元素的下拉列表:.这些值不在数据库或其他信息存储库中.

I have an internal application that I needs to have a drop down list for two date type elements: Month and Year. These values are not in a database or other repository of information.

我知道我可以通过将它们添加到类似对象的字典中来设置包含我需要的值的列表(我需要将月份与数字表示相关联,一月 => 01):

I know I could just setup a list with the values I need by adding them to a dictionary like object (I need to correlate the Month to the numerical representation, January => 01):

var months = new Dictionary<String,String>();
months.Add("01", "January");
...

年份的下拉列表会更容易一些,因为我可以选择一个起始年份并在通用列表中迭代到当前或当前 +1 年.

The drop down list for the year will be a bit easier as I can just choose a starting year and iterate up to the current or current+1 year in a generic list.

有没有更好的方法来处理这些数据元素?内置的东西,或者我应该实现的好的设计模式?

Is there a better way to handle these data elements? Something built in, or a good design pattern that I should be implementing?

推荐答案

您可以使用它来获取所有月份名称的列表并循环遍历它.

You could use this to get a list of all the Month names and loop through it.

CultureInfo.CurrentCulture.DateTimeFormat.MonthNames

您可以像这样使用它...使用月份的索引作为下拉列表的值

You can use it like this...using the index of the Month as the value for your dropdown

var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
for (int i = 0; i < months.Length; i++)
{
     ddl.Items.Add(new ListItem(months[i], i.ToString()));
}

这篇关于为 ASP.NET 编写月份和年份下拉列表的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆