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 编写月份和年份下拉列表的最佳方法是
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
