What is the best way to code up a Month and Year drop down list for ASP.NET?(为 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 编写月份和年份下拉列表的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 ASP.NET 编写月份和年份下拉列表的最佳方法是


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01