繁体   English   中英

以编程方式在asp.net日历中选择日期

[英]Select dates in asp.net Calendar programatically

我在asp.net网络表单应用程序中使用Calendar控制器。 我跟随本文在应用程序中实现Calendar 我将选定的日期添加到List<DateTime>以记住选定的日期并在以后的操作中使用它们。

现在,我在页面上添加了“ Select Weekends ,“ Select Weekdays ,“ Select Month和“ Select Year等按钮。

  • 如果单击“ Select Weekends按钮,则需要选择当月的所有周末并将其添加到List<DateTime>

  • 如果单击“ Select Weekdays按钮,则需要选择当月的所有工作日并将其添加到List<DateTime>

  • 如果单击“ Select Month **”按钮,则需要Select Month所有天并将其添加到List<DateTime>

  • 如果单击“ Select Year按钮,则需要选择当年的所有天并将其添加到List<DateTime>

如何使用C#以编程方式执行此操作?

我认为没有什么奇迹的解决方案,这是我如何在周末的日子里为您想要的方法编写2种方法。 对于其他几点,您可以或多或少地做同一件事:

    protected void WeekendDays_Button_Click(object sender, EventArgs e)
    {
        this.SelectWeekEnds():
    }

    private void SelectWeekEnds(){
        //If you need to get the selected date from calendar
        //DateTime dt = this.Calendar1.SelectedDate;

        //If you need to get the current date from today
        DateTime dt = DateTime.Now;

        List<DateTime> weekendDays = this.SelectedWeekEnds(dt);
        weekendDays.ForEach(d => this.Calendar1.SelectedDates.Add(d));
    }

    private List<DateTime> GetWeekEndDays(DateTime DT){
        List<DateTime> result = new List<DateTime>();
        int month = DT.Month;
        DT = DT.AddDays(-DT.Day+1);//Sets DT to first day of month

        //Sets DT to the first week-end day of the month;
        if(DT.DayOfWeek != DayOfWeek.Sunday)
            while (DT.DayOfWeek != DayOfWeek.Saturday)
                DT = DT.AddDays(1);

        //Adds the week-end day and stops when next month is reached.
        while (DT.Month == month)
        {
            result.Add(DT);
            DT = DT.AddDays(DT.DayOfWeek == DayOfWeek.Saturday ? 1 : 6);
        }
        return result;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM