什么是编写一个月份和年份下拉列表ASP.NET的最好方法? [英] What is the best way to code up a Month and Year drop down list for 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屋!