问题描述
我有一个内部应用程序,我需要一个包含两个日期类型元素的下拉列表:月和年.这些值不在数据库或其他信息存储库中.
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 编写月份和年份下拉列表的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!