2010-03-25 34 views
6

Tôi không chắc tại sao những người khác chưa hỏi câu hỏi này trước đây. Nhưng bạn có nhận thấy rằng asp: Lịch hiển thị thêm một tuần ở cuối?Cách xóa tuần cuối cùng của một lịch

Ví dụ: nếu VisibleMonth được đặt thành 2010-03-01 và FirstDayOfWeek đến Chủ nhật: Nó sẽ hiển thị 6 tuần.

  1. 28 Tháng Hai - 6 tháng 3
  2. 7 Tháng Ba - 13 Tháng 3
  3. 14 Tháng Ba - 20 Tháng Ba
  4. Tháng Ba 21 - tháng ba 27
  5. 28 tháng ba - 3 tháng tư
  6. ngày 04 tháng 4 đến tháng 10

Tôi đã tự hỏi tại sao Microsoft hiển thị Dòng cuối cùng hoàn toàn vào tháng Tư. Tôi đã cố gắng tìm kiếm mạng cho một tài sản nhưng nó dường như không tồn tại.

Giải pháp duy nhất mà tôi có thể nghĩ đến là ghi đè Pre_Render và kiểm tra tất cả các ngày riêng lẻ nếu chúng vẫn còn trong tuần của VisibleDate. Nhưng tất nhiên đó là một kiểm tra cực đoan kể từ khi mỗi rendering của điều khiển cho thấy nó.

Đây là công việc của tôi.

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
{ 
    int dayOfWeek = Convert.ToInt16(e.Day.Date.DayOfWeek); 
    int compensate = dayOfWeek - Convert.ToInt16(DayOfWeek.Sunday); 
    DateTime WeekStart = e.Day.Date.AddDays(-1 * compensate); 
    DateTime WeekEnd = WeekStart.AddDays(6); 

    // If the start and end of the week does not have relevance to the current month 
    if (WeekStart.Month != Calendar1.VisibleDate.Month && 
     WeekEnd .Month != Calendar1.VisibleDate.Month) 
    { 
     e.Cell.Text = ""; 
     e.Cell.Height = 0; 
     e.Cell.Visible = false; 
    } 
} 

Trả lời

7

rất đẹp. Hoạt động với hầu hết các trình duyệt nhưng chạy nhanh với Chrome 11.0.696.71 Và Safari dành cho cửa sổ (không được thử nghiệm trên mac)

Trong một vài tháng, điều khiển lịch hiển thị thêm tuần vào đầu tháng. (khi ngày 1 của tháng là ngày đầu tiên của tuần.)

Khi bạn đặt e.cell.Visible = false nó sẽ không hiển thị phần tử. Vì vậy, trong chrome bạn kết thúc với một hàng <tr></tr>. Chrome hiển thị mục này dưới dạng hàng trống. Và vì tôi không nghĩ rằng có một cách để thiết lập chiều cao/kiểu phần tử TR thông qua điều khiển lịch, bạn kết thúc với một lịch trông xấu xí mà thiếu hàng đầu tiên của nó trong một số tháng nhất định.

Ngoài ra chiều cao setitng thành 0 không có gì khi bạn đặt Hiển thị = false. Nếu bạn không đặt Hiển thị = false và chỉ đặt chiều cao = 0 thì nó vẫn không hiển thị chính xác trong chrome. Vì vậy, giải pháp là đặt chiều cao thành

Đây là giải pháp đã sửa đổi của tôi.

các onrowrender

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e){ 
    hideExtraWeek(sender, e, (DayOfWeek)Calendar1.FirstDayOfWeek); 
} 

chức năng

protected void hideExtraWeek(object sender, DayRenderEventArgs e, DayOfWeek dw){ 
     if (dw == (DayOfWeek)7) dw = (DayOfWeek)0; // FirstDayOfweek returns 7 when set to default, But it's zero based so valid values are 0 to 6 
     Boolean blnBrowserDoesntSupportsEmptyRow= Request.Browser.Browser=="Chrome" || 
              Request.Browser.Browser=="Safari"; 

     int dayOfWeek = Convert.ToInt16(e.Day.Date.DayOfWeek); 
     int compensate = dayOfWeek - Convert.ToInt16(dw); 
     DateTime WeekStart = e.Day.Date.AddDays(-1 * compensate); 
     DateTime WeekEnd = WeekStart.AddDays(6); 

     // If the start and end of the week does not have relevance to the current month 
     if (WeekStart.Month==WeekEnd.Month && e.Day.IsOtherMonth){ 
      e.Cell.Text = ""; 
      e.Cell.Height = 1; // fix for chrome. Visible=false leaves a blank row even when there are no <td>s in the <tr> 
      e.Cell.Visible = blnBrowserDoesntSupportsEmptyRow; 
     } 
    } 
1

Ok tôi có một giải pháp khác. Sau khi chỉnh sửa một số. Hy vọng nó giúp ích. Nó có chức năng nhiều hơn một chút nhưng nếu lịch của bạn được nhiều lượt truy cập, nó chỉ có thể tiết kiệm 1% của CPU :). Đảm bảo gắn sự kiện Calendar1_VisibleMonthChanged để OnVisibleMonthChanged

private DateTime fromDate; 
private DateTime toDate; 
private Boolean blnBrowserDoesntSupportsEmptyRow = Request.Browser.Browser=="Chrome" || 
                 Request.Browser.Browser=="Safari"; 

    protected void Page_Load(object sender, EventArgs e){ 
     if (!Page.IsPostBack){ 
      setFromToDates(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)); 
     } 
    } 

    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e){ 
     setFromToDates(e.NewDate); 
    } 

    protected void setFromToDates(DateTime monthStart){ 

     DayOfWeek dw = (DayOfWeek)Calendar1.FirstDayOfWeek; 
     if (dw == (DayOfWeek)7) dw = (DayOfWeek)0; 


     if (monthStart.DayOfWeek == dw){ 
      this.fromDate = monthStart;// if 1st day of calendar is also 1st of the month. just set as is 
     }else{ 
      int dayOfWeek = Convert.ToInt16(monthStart.DayOfWeek); 
      dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek; 
      int compensate = dayOfWeek - Convert.ToInt16(dw); 
      this.fromDate = monthStart.AddDays(-1 * compensate);// set FromDate to the beggning day of the calendar. I.e may start from e.g. 25th of the previous month 
     } 

     this.toDate = monthStart.AddMonths(1); 
     if (this.toDate.DayOfWeek != dw) 
     { 
      int dayOfWeek = Convert.ToInt16(this.toDate.DayOfWeek); 
      dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek; 
      int compensate = dayOfWeek - Convert.ToInt16(dw); 
      this.toDate=this.toDate.AddDays(7-compensate); 
     } 
    } 








    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e){ 
     // hide extra week 
     hideExtraWeek(sender, e); 
    } 

    // returns weather or not the given day is hidden 
    protected Boolean hideExtraWeek(object sender, DayRenderEventArgs e){ 
      Boolean isVisibleDay = e.Day.Date >= this.fromDate && e.Day.Date < this.toDate; 

      // If the start and end of the week does not have relevance to the current month 
      if (!isVisibleDay){ 
       e.Cell.Text = ""; 
       e.Cell.Height = 1; // fix for chrome. Visible=false leaves a blank row even when there are no <td>s in the <tr> 
       e.Cell.Visible = blnBrowserDoesntSupportsEmptyRow; 
      } 
     return !isVisibleDay; 
    } 
1
<asp:Calendar ID="Calendar1" runat="server" CellPadding="1" CellSpacing="0" 
Width="600px" FirstDayOfWeek="Monday" BorderColor="#a1a1a1" 
BorderWidth="1" BorderStyle="None" ShowGridLines="True" ShowDescriptionAsToolTip="True" 
NextPrevStyle-CssClass="" 
Height="500px" OnDayRender="Calendar1_DayRender" 
SelectionMode="None" NextMonthText="&amp;gt;&amp;gt;" PrevMonthText="&amp;lt;&amp;lt;"> 
<OtherMonthDayStyle BorderStyle="None" /> 
</asp:Calendar> 

bool weekstart = false; 
int[] longmonths = new int[] { 1, 3, 5, 7, 8, 10, 12 };// 31 days in a Month 
int[] shortmonths = new int[] { 4, 6, 9, 11 };// 30 days in a Month 

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
{ 

    if (e.Day.IsOtherMonth) 
    { 
     e.Cell.Text = String.Empty; 
     e.Cell.Height = 0; 
     if (e.Day.Date.DayOfWeek == DayOfWeek.Monday)//Monday is FirstDayOfWeek 
     { 
      if (shortmonths.Contains(e.Day.Date.Month) && e.Day.Date.Day == 24) 
      { 
      weekstart = true; 
      } 
      else if (longmonths.Contains(e.Day.Date.Month) && e.Day.Date.Day == 25) 
      { 
      weekstart = true; 
      } 
     } 
     if (weekstart) 
     { 
      e.Cell.Visible = false; 
     } 
    } 
    else if (!e.Day.IsOtherMonth) 
    { 
     weekstart = false; 
    } 

} 
1

Đây là mã VB tôi sử dụng. Nó sẽ loại bỏ các hàng trống ở cả trên và dưới khi cần thiết.Nó sử dụng một biến riêng. Phương pháp này dường như không có bất kỳ sự cố nào trong Chrome hoặc Safari.

Private _hideEmptyWeek As Boolean 

Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender 

    If e.Day.IsOtherMonth Then 

     '' Hide first week if empty 
     If e.Day.Date = e.Day.Date.AddDays(-e.Day.Date.Day + 1).AddMonths(1).AddDays(-7) Then ' If this date is a full week before next month 
      _hideEmptyWeek = True 
     End If 


     '' Hide last week if empty 
     If e.Day.Date.DayOfWeek = DayOfWeek.Sunday And e.Day.Date.Day < 7 Then ' If this is the first Sunday of next month 
      _hideEmptyWeek = True 
     End If 


     '' Hide cell if we are in an empty week 
     If _hideEmptyWeek = True Then 
      e.Cell.Visible = False 
     End If 
    Else 
     _hideEmptyWeek = False 
    End If 
End Sub 
2

Nếu bạn có SelectWeekText và SelectionMode = "DayWeek" hoặc "DayWeekMonth", bạn sẽ gặp sự cố với đánh dấu lựa chọn tuần vẫn hiển thị cho tuần bị ẩn. Đây là cách tôi đã làm nó với một chút JQuery.

Sub cal_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs) 
     If HideExtraWeek(e, If(Cal.FirstDayOfWeek = WebControls.FirstDayOfWeek.Default, Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FirstDayOfWeek, Cal.FirstDayOfWeek)) Then 
      e.Cell.Style("display") = "none" 
      e.Cell.CssClass = "hiddenWeek" 
      Exit Sub 
     End If 
     'do other render stuff here 
End Sub 

Private Function HideExtraWeek(ByVal e As DayRenderEventArgs, ByVal startOfWeekDay As Integer) As Boolean 
    If e.Day.IsOtherMonth Then 
     'hide empty weeks, logic credited to Robert 
     Dim currDay As Integer = e.Day.Date.DayOfWeek 
     Dim weekStart As DateTime = e.Day.Date.AddDays(startOfWeekDay - currDay) 'first day of the week 
     Dim weekEnd As DateTime = weekStart.AddDays(6) 

     Return (weekStart.Month = weekEnd.Month) 'the entire week is part of the other month 
    End If 
    Return False 
End Function 

<script type="text/javascript"> 
     $(document).ready(function() { 
      $("td.hiddenWeek").parent().hide(); 
     } 
</script> 
+0

Cảm ơn bạn! đây là những gì tôi đang tìm kiếm vì đánh dấu lựa chọn tuần. Đã phải thêm (Nếu currDay = 0 Sau đó, currDay = 7) vào hàm HideExtraWeek để quản lý thứ hai là ngày đầu tiên của tuần. Có lẽ không phải là giải pháp sạch nhất nhưng làm việc cho tôi. – Michelh91

Các vấn đề liên quan