Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c# - 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.

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");
...

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...