簡體   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