使用接下来的两个工作周 c# 填充下拉列表

本文关键字:工作周 两个 填充 下拉列表 接下来 | 更新日期: 2023-09-27 18:32:15

我正在尝试在 C# asp.net Web 表单中创建一个下拉列表,该列表填充了本周的 mon-sat 和下周将在约会计划表单中使用的。

public void Datetime_Fill()
{
    DateTime Date = DateTime.Now;
    int sunday_count = 0;
    while(sunday_count < 2)
    {
        if (Date.DayOfWeek != DayOfWeek.Sunday)
        {
            //Console.WriteLine("The day of the week for {0:d} is {1}.", Date, Date.DayOfWeek);
            ddl_Appt_Date.Items.Add(new ListItem(Date.DayOfWeek.ToString()));
            Date.AddDays(1);
        }
        else
        {
            Date.AddDays(1);
            sunday_count++;
        }
    }
}

我在页面加载时执行此命令,每当我加载它时,我的网页根本不显示,浏览器坐在那里加载。我假设 Date.DayOfWeek 永远不会等于 DayOfWeek.Sunday,因此它永远不会离开 while 循环。我把Console.WriteLine放在那里只是为了故障排除,但仍然没有显示任何内容。

使用接下来的两个工作周 c# 填充下拉列表

你会想做的

Date = Date.AddDays(1);

因为Date永远不会更新。

从 MSDN 日期时间 :

DateTime.AddDays() returns a new DateTime that adds the specified number of days to the value of this instance.

另一种解决方案可能是在数据库上有一个表,其中包含要排除的所有日期。这样,您还可以预测一周中是否有假期或类似的事情。

现在的方式,你只是在周日起飞。这是你真正需要的吗?

此外,您应该在所有下拉列表中使用valuetext

例如:

    DropDownList1.Items.Add(new ListItem("<text>", value));

您必须保存 addDay 函数的返回值才能更新日期对象并到达星期日,因此:

日期 = 日期.添加天数(1);