[英]populate drop down list with month/year
I have a date and I need to populate a drop-down with the months/years between that date and today.我有一个日期,我需要用该日期和今天之间的月/年填充下拉列表。 For instance, if that date is 10/14/2010 then the drop-down should contain October 2010, November 2010, December 2010, January 2011.
例如,如果该日期是 10/14/2010,则下拉列表应包含 2010 年 10 月、2010 年 11 月、2010 年 12 月、2011 年 1 月。
The way I'm thinking of doing this is to pass that date to a function, loop from today backwards step 1 month while adding each month to a collection until we reach that date and finally return a collection of strings.我想这样做的方法是将该日期传递给一个函数,从今天向后循环 1 个月,同时将每个月添加到一个集合中,直到我们到达该日期,最后返回一个字符串集合。 Then, populate the drop-down control on page load.
然后,在页面加载时填充下拉控件。 Finally, use some ajax with a page method to parse back the string and trigger a partial page reload.
最后,使用一些带有 page 方法的 ajax 来解析字符串并触发部分页面重新加载。
I'm just wondering if there's an easy way to do it.我只是想知道是否有一种简单的方法可以做到这一点。
Thanks.谢谢。
For Year,年,
public static IEnumerable<int> Range (int start, int count)
{
int end = start + count;
for (int i = start; i < end; i++)
yield return i;
}
var startYear = 2000;
YearDropDownList.ItemsSource= Enumerable.Range(startYear, 2050 - startYear + 1);
For Month, An enumerable list with .ToString("MMMM") format.对于 Month,使用 .ToString("MMMM") 格式的可枚举列表。
You can do something like this which is pretty much what you described except counting forward:你可以做这样的事情,除了向前计数外,这几乎是你所描述的:
private string[] FillDropDownWithDates(DateTime dt)
{
DateTime dtnow = DateTime.Now;
List<string> values = new List<string>();
if ( (dt <= dtnow))
{
values.Add(String.Format("{0:y}", dt));
}
while ( (dt = dt.AddMonths(1)) <= dtnow || ( dt.Month == dtnow.Month && dt.Year == dtnow.Year) )
{
values.Add(String.Format("{0:y}", dt)); // "March, 2008" YearMonth
}
return values.ToArray();
}
Maybe you can try this:也许你可以试试这个:
static IEnumerable<DateTime> monthsBetween(DateTime startDate, DateTime endDate)
{
return Enumerable.Range(0, (endDate.Year - startDate.Year) * 12 + (endDate.Month - startDate.Month + 1))
.Select(m => new DateTime(startDate.Year, startDate.Month, 1).AddMonths(m));
}
This will not give you the result in the exact format that you want, but you get the drift.这不会以您想要的确切格式为您提供结果,但您会得到偏差。 :)
:)
public static List<string> GetMonths(DateTime StartDate)
{
List<string> MonthList = new List<string>();
DateTime ThisMonth = DateTime.Now.Date;
while (ThisMonth.Date > StartDate.Date)
{
MonthList.Add(ThisMonth.ToString("MMMM") + " " + ThisMonth.Year.ToString());
ThisMonth = ThisMonth.AddMonths(-1);
}
return MonthList;
}
This is how I got a 12 month/year.这就是我获得 12 个月/年的方式。 Hope the code helps.
希望代码有帮助。
public IEnumerable<SelectListItem> Additional12Months {
get
{
return Enumerable.Range(12, 12).Select(i => new SelectListItem { Value = DateTime.Now.AddMonths(-(i)).ToShortDateString(), Text = DateTime.Now.AddMonths(-(i)).ToString("MMM-yyyy") }).ToList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.