简体   繁体   English

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

[英]Select dates in asp.net Calendar programatically

I'm using Calendar controller in my asp.net web forms application. 我在asp.net网络表单应用程序中使用Calendar控制器。 I followed this article to implement Calendar in my application. 我跟随本文在应用程序中实现Calendar I'm adding selected days into a List<DateTime> to remember selected dates and use them in future actions. 我将选定的日期添加到List<DateTime>以记住选定的日期并在以后的操作中使用它们。

Now I have added buttons to my page like Select Weekends , Select Weekdays , Select Month and Select Year . 现在,我在页面上添加了“ Select Weekends ,“ Select Weekdays ,“ Select Month和“ Select Year等按钮。

  • if I click on the Select Weekends button, I need to select all weekend days of current month and add them into List<DateTime> . 如果单击“ Select Weekends按钮,则需要选择当月的所有周末并将其添加到List<DateTime>

  • if I click on the Select Weekdays button, I need to select all week days of current month and add them into List<DateTime> . 如果单击“ Select Weekdays按钮,则需要选择当月的所有工作日并将其添加到List<DateTime>

  • if I click on the Select Month ** button, I need to select all days of the current month and add them into List<DateTime> . 如果单击“ Select Month **”按钮,则需要Select Month所有天并将其添加到List<DateTime>

  • if I click on the Select Year button, I need to select all days of the current year and add them into List<DateTime> . 如果单击“ Select Year按钮,则需要选择当年的所有天并将其添加到List<DateTime>

How can I do this programatically using C#? 如何使用C#以编程方式执行此操作?

I don't think there's a miracle solution, here how I would write 2 methods to what you want for the weekend days. 我认为没有什么奇迹的解决方案,这是我如何在周末的日子里为您想要的方法编写2种方法。 For the other points, you could do more or less the same thing: 对于其他几点,您可以或多或少地做同一件事:

    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