Asp.net 如何删除日历的最后一周

Asp.net 如何删除日历的最后一周,asp.net,calendar,Asp.net,Calendar,我不知道为什么其他人以前没有问过这个问题。但是你有没有注意到asp:Calendar在末尾显示了额外的一周 例如,如果VisibleMonth设置为2010-03-01,FirstDayOfWeek设置为Sunday: 它将显示6周 2月28日至3月6日 三月七日至十三日 三月十四日至二十日 三月二十一日至二十七日 三月二十八日至四月三日 四月四日至四月十日 我想知道为什么微软显示的最后一行完全是在四月。我试图在网上搜索一处房产,但它似乎不存在 我能想到的唯一解决方案是覆盖Pre_渲染,并检查所

我不知道为什么其他人以前没有问过这个问题。但是你有没有注意到asp:Calendar在末尾显示了额外的一周

例如,如果VisibleMonth设置为2010-03-01,FirstDayOfWeek设置为Sunday: 它将显示6周

  • 2月28日至3月6日
  • 三月七日至十三日
  • 三月十四日至二十日
  • 三月二十一日至二十七日
  • 三月二十八日至四月三日
  • 四月四日至四月十日
  • 我想知道为什么微软显示的最后一行完全是在四月。我试图在网上搜索一处房产,但它似乎不存在

    我能想到的唯一解决方案是覆盖Pre_渲染,并检查所有单个日期是否仍在VisibleDate的一周内。但当然,这是一个极端的检查,因为控件的每次呈现都会显示它

    这是我的工作

    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;
        }
    }
    

    非常好。适用于大多数浏览器,但与Chrome 11.0.696.71和Safari for windows兼容(未在mac上测试)

    对于某些月份,日历控件在月初显示额外的一周。(当一个月的第一天是一周的第一天时。)

    当设置e.cell.Visible=false时,它不会渲染元素。因此,在chrome中,您将得到一行
    。Chrome将此渲染为空白行。由于我认为没有办法通过calendar控件来设置TR元素的高度/样式,因此最终会出现一个外观难看的日历,它在某些月份缺少第一行

    此外,当设置Visible=false时,将高度设置为0不会产生任何作用。如果不将Visible设置为false,只将height设置为0,则仍然无法在chrome中正确渲染。因此,解决方案是将高度设置为1

    这是我修改过的解决方案

    onrowrender

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

        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;
            }
        }
    
    受保护的void hideExtrawWeek(对象发送方、DayRenderReventArgs e、DayOfWeek dw){
    如果(dw==(DayOfWeek)7)dw=(DayOfWeek)0;//FirstDayOfweek设置为默认值时返回7,但它是基于零的,因此有效值为0到6
    布尔blnBrowserDoesntSupportsEmptyRow=Request.Browser.Browser==“Chrome”||
    Request.Browser.Browser==“Safari”;
    int dayOfWeek=转换为NT16(即Day.Date.dayOfWeek);
    int compensate=星期几-转换为16(dw);
    DateTime WeekStart=e.Day.Date.AddDays(-1*天);
    DateTime WeekEnd=WeekStart.AddDays(6);
    //如果一周的开始和结束与当前月份不相关
    if(周开始月==周末月和e日等温线月){
    e、 Cell.Text=“”;
    e、 Cell.Height=1;//fix for chrome.Visible=false将保留一个空行,即使在
    e、 单元格可见=blnBrowserDoesntSupportsEmptyRow;
    }
    }
    
    好的,我有另一个解决方案。经过一些调整。希望能有帮助。它的功能稍微多一些,但如果你的日历点击次数太多,它可能只节省1%的cpu:)确保将
    日历1\u 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;
        }
    
    private DateTime fromDate;
    私有日期时间;
    私有布尔blnBrowserDoesntSupportsEmptyRow=Request.Browser.Browser==“Chrome”||
    Request.Browser.Browser==“Safari”;
    受保护的无效页面加载(对象发送方、事件参数e){
    如果(!Page.IsPostBack){
    setFromToDates(新日期时间(DateTime.Today.Year,DateTime.Today.Month,1));
    }
    }
    受保护的无效日历1\u VisibleMonthChanged(对象发送者,MonthChangedEventArgs e){
    setFromToDates(如NewDate);
    }
    受保护的无效集合FromToDates(DateTime monthStart){
    DayOfWeek dw=(DayOfWeek)日历1.FirstDayOfWeek;
    如果(dw==(DayOfWeek)7)dw=(DayOfWeek)0;
    如果(monthStart.DayOfWeek==dw){
    this.fromDate=monthStart;//如果日历的第一天也是一个月的第一天,只需按原样设置即可
    }否则{
    int dayOfWeek=转换为NT16(monthStart.dayOfWeek);
    dayOfWeek=dayOfWeek==0?7:dayOfWeek;
    int compensate=星期几-转换为16(dw);
    this.fromDate=monthStart.AddDays(-1*补偿);//将fromDate设置为日历的乞讨日。例如,可以从上个月的25日开始
    }
    this.toDate=monthStart.AddMonths(1);
    if(this.toDate.DayOfWeek!=dw)
    {
    int dayOfWeek=Convert.ToInt16(this.toDate.dayOfWeek);
    dayOfWeek=dayOfWeek==0?7:dayOfWeek;
    int compensate=星期几-转换为16(dw);
    this.toDate=this.toDate.AddDays(7-7天);
    }
    }
    受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e){
    //多藏一周
    hideExtraWeek(发送方,e);
    }
    //返回给定日期是否隐藏的天气
    受保护的布尔HideExtrawEk(对象发送方,DayRenderEventArgs e){
    布尔值isVisibleDay=e.Day.Date>=this.fromDate&&e.Day.Date
    
    bool weekstart=false;
    int[]longmounts=newint[]{1,3,5,7,8,10,12};//一个月31天
    int[]shortmonths=newint[]{4,6,9,11};//一个月30天
    受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
    {
    if(如:日等温线)
    {
    e、 Cell.Text=String.Empty;
    e、 单元高度=0;
    如果(e.Day.Date.DayOfWeek==DayOfWeek.Monday)//星期一是星期一的第一天
    {
    如果(上海)
    
    <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;
       }
    
    }
    
    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
    
     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>