问题描述
这是我的代码来获取3个日期(12.5.2011,12.4.2011,12.3.2011)并在dropdwonlist中显示tat.我想显示数据库中有数据的日期.否则我想显示之前的3个日期( 12.4.2011、12.3.2011和12.2.2011)正在数据库中存储数据.如何实现此目标...
谁能说出要解决的主意..
Hi,
Here is my code to get 3 dates(12.5.2011,12.4.2011,12.3.2011) and display tat in dropdwonlist.I want to show the date which is having data in database.otherwise i want to show the previous 3 dates(12.4.2011,12.3.2011 and 12.2.2011) which is having data in database.how can i achieve this...
can any one tell idea to solve..?
string[] date = new string[8];
DateTime dt = DateTime.UtcNow.AddHours(5.5);
DropDownList1.Items.Insert(0, "--SELECT DATE--");
DropDownList1.Items.Insert(1, new ListItem(dt.ToString("MM/dd/yyyy")));
for (int i = 1; i < 3; i++)
{
string s = dt.AddDays(-i).ToString("MM/dd/yyyy");
DropDownList1.Items.Insert(1 + i, new ListItem(s, s));
}
推荐答案
DataTable dt = GetDataFromDataBase();
if(dt.Rows.Count == 0)
{
dt.Row.Add(new object[]{"12.5.2011"});
...
}
在后台代码中
}
In code-behind
DropDownList1.DataSource = dt;
将项目按顺序添加到DropDownList Items集合中时,无需使用插入.使用Add方法会更快.
When adding items to the DropDownList Items collection in sequential order it is not necessary to use Insert. Using the Add method will be faster.
这篇关于基于数据库记录的下拉列表中的加载日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!