C# 为 ASP.NET 编写月份和年份下拉列表的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/812330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to code up a Month and Year drop down list for ASP.NET?
提问by Bryan Rehbein
I have an internal application that I needs to have a drop down list for two date type elements: Monthand Year. These values are not in a database or other repository of information.
我有一个内部应用程序,我需要有两个日期类型元素的下拉列表: Month和Year。这些值不在数据库或其他信息存储库中。
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):
我知道我可以通过将它们添加到类似对象的字典中来设置包含我需要的值的列表(我需要将月份与数字表示相关联,一月 => 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.
年份的下拉列表会更容易一些,因为我可以选择一个起始年份并在通用列表中迭代到当前或当前+1 年。
Is there a better way to handle these data elements? Something built in, or a good design pattern that I should be implementing?
有没有更好的方法来处理这些数据元素?内置的东西,或者我应该实现的好的设计模式?
采纳答案by Jab
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()));
}
回答by Cyberherbalist
Extending @Jesse Brown's answer...
扩展@Jesse Brown 的回答...
With a using System.Globalizationdirective, I have the following code:
使用using System.Globalization指令,我有以下代码:
for (int x = 0; x < 12; x++)
{
cboMonth.Items.Add
(
(x+1).ToString("00")
+ " "
+ CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(x)
);
}
This produces a dropdown list that looks like:
这会生成一个下拉列表,如下所示:
01 January 02 February 03 March ... 12 December
1 月 1 日 2 月 2 日 3 月 3 日 ... 12 月 12 日
A further refinement might be to make the displayed month the current month by adding:
进一步的改进可能是通过添加以下内容使显示的月份成为当前月份:
cboMonth.Text = DateTime.Now.Month.ToString("00")
+ " "
+ CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(DateTime.Now.Month);
After the for loop.
在 for 循环之后。
回答by Bryan Rehbein
Here is my solution, which is very similar to @jesse-brown's solution (the accepted answer)
这是我的解决方案,它与@jesse-brown 的解决方案非常相似(已接受的答案)
VB.NET:
VB.NET:
In a global functions class:
在全局函数类中:
Public Shared Function GetMonthList() As Generic.Dictionary(Of String, String)
Dim months As New Generic.Dictionary(Of String, String)()
For m As Int32 = 1 To 12
months.Add(String.Format("{0:0#}", m), CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m))
Next
Return months
End Function
On the ASPX page:
在 ASPX 页面上:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ddMonth.DataSource = GlobalFunctions.GetMonthList()
ddMonth.DataValueField = "Key"
ddMonth.DataTextField = "Value"
ddMonth.DataBind()
End Sub
This implementation is in VB.NET because that happens to be what this webapp is using (legacy), however thank you very much for the examples in C# (my preferred language), I'm posting the VB.NET here to help the VB.NET community as well.
这个实现在 VB.NET 中,因为这恰好是这个 webapp 正在使用的(遗留),但是非常感谢你在 C#(我的首选语言)中的例子,我在这里发布 VB.NET 以帮助 VB .NET 社区也是如此。
回答by Simon_Weaver
For ASP.NET MVC this is what I'm doing.
对于 ASP.NET MVC,这就是我正在做的。
Note I prefer to use a codebehindfor things like this - its still part of the view and there's nothing wrong with the view constructing a SelectList.
注意我更喜欢使用代码隐藏来处理这样的事情 - 它仍然是视图的一部分,并且构建 SelectList 的视图没有任何问题。
PaymentControl.ascx
PaymentControl.ascx
<%= Html.DropDownList("ExpirationMonth", ExpirationMonthDropdown)%> /
<%= Html.DropDownList("ExpirationYear", ExpirationYearDropdown)%>
PaymentControl.ascx.cs
PaymentControl.ascx.cs
public partial class PaymentControl : ViewUserControl<CheckoutModel>
{
public IEnumerable<SelectListItem> ExpirationMonthDropdown
{
get
{
return Enumerable.Range(1, 12).Select(x =>
new SelectListItem()
{
Text = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[x - 1] + " (" + x + ")",
Value = x.ToString(),
Selected = (x == Model.ExpirationMonth)
});
}
}
public IEnumerable<SelectListItem> ExpirationYearDropdown
{
get
{
return Enumerable.Range(DateTime.Today.Year, 20).Select(x =>
new SelectListItem()
{
Text = x.ToString(),
Value = x.ToString(),
Selected = (x == Model.ExpirationYear)
});
}
}
}
回答by Shabbir J
Below code is for load month dropdown as Select
下面的代码是加载月份下拉选择
private void LoadMonth()
{
ddlmonth.Items.Add(new ListItem("Select", 0.ToString()));
var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
for (int i = 0; i < months.Length-1; i++)
{
ddlmonth.Items.Add(new ListItem(months[i], (i+1).ToString()));
}
}
回答by user6462359
You can try this... Ref Here:
你可以试试这个……参考这里:
http://allinworld99.blogspot.com/2015/01/bind-monthsyear-dropdownlist-c-aspnet.html
http://allinworld99.blogspot.com/2015/01/bind-monthsyear-dropdownlist-c-aspnet.html
for (int i = 1; i <= 12; i++)
{
drpBMonth.Items.Add(new System.Web.UI.WebControls.ListItem(DateTimeFormatInfo.CurrentInfo.GetMonthName(i), i.ToString()))};
回答by prasanth
public void bind_month()
{
var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
ddl_month.Items.Insert(0, new ListItem("--Select--", "0"));
for (int i = 0; i < months.Length-1; i++)
{
ddl_month.Items.Add(new ListItem(months[i],(i+1).ToString()));
}
}
回答by JIYAUL MUSTAPHA
/Try this code to bind all month in dropdown list without using database/
/试试这个代码在不使用数据库的情况下绑定下拉列表中的所有月份/
public void GetMonthList(DropDownList ddl)
{
DateTime month = Convert.ToDateTime("1/1/2000");
for (int i = 0; i < 12; i++)
{
DateTime NextMont = month.AddMonths(i);
ListItem list = new ListItem();
list.Text = NextMont.ToString("MMMM");
list.Value = NextMont.Month.ToString();
ddl.Items.Add(list);
}
//ddl.Items.Insert(0, "Select Month");
ddl.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}