在下拉列表框中进行循环? [英] for loop in dropdownlist box ?

查看:108
本文介绍了在下拉列表框中进行循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在下拉列表框中使用for循环填充1到30的月份?

how to use for loop in drop down list box to fill the month days from 1-30 ?

推荐答案

下面是一个示例代码,用于填充基于当前日期的月份中的天.

Here is a sample code that fills the dropdown with the days of month based on current date.

DateTime date = DateTime.Now;

 for (int i = 1; i <= DateTime.DaysInMonth(date.Year, date.Month); i++)
 {
     YourDropdownID.Items.Add(new ListItem(i.ToString(), i.ToString()));
 }


大家好,

@_TR__:好的答案.我的5 :)但是一件小事.

既然如此,该问题被标记为"C#4.0",这是您的解决方案的改进版本.
Hey everyone,

@_TR__: Nice answer. My 5 :) But one small thing.

Since, the question is tagged "C# 4.0" here is an improved version of your solution.
DateTime today = DateTime.Now;
int days = DateTime.DaysInMonth(today.Year, today.Month);
Enumerable.Range(1, days).ToList().ForEach(i => YourDropdownID.Items.Add(i));


希望对您有所帮助


Hope this helps, regards


如果"ddl"是下拉列表的ID,请使用以下代码:
If "ddl" is the id of the dropdownlist use the code below:
for (int i = 1; i < 31; i++)
  {
     ddl.Items.Add(i.ToString());
  }


这篇关于在下拉列表框中进行循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆