禁用Calendar控件中的上个月链接

时间:2014-03-07 14:24:11

标签: c# .net .net-4.0 calendar web-controls

我正在尝试找到一种方法来禁用Calendar控件中的“上个月”链接。这似乎应该是一个非常简单的任务,但我在文档中找不到任何属性或方法来执行此操作。

请注意,我正在寻找一种方法来禁用仅上一个月链接,而不是两个链接。仅禁用“下个月”链接应该是相同的解决方案,因此也可以接受。还请注意ShowNextPrevMonth属性是一个不可行的解决方案,因为它用于隐藏,这是可以接受但不太理想的,并隐藏两个链接,而不是只隐藏一个。

.NET Calendar Control Documentation


谢谢你的帮助!快乐的编码!


旁注:我不是在寻找JavaScript解决方案,因为我可以简单地设计一个。但是,如果有人确实知道如何在每个链接上应用不同的CssClass,我会向该投票推荐。

1 个答案:

答案 0 :(得分:0)

   <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.NET Prevent Previous Month Selection - Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Calendar ID="someCalendar" runat="server"
     OnVisibleMonthChanged="theVisibleMonthChanged" />
    </div>
    </form>
</body>
</html>




And the C# code for my demo is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Demo to show how one might prevent the user from selecting 
/// the previous month in ASP.NET
/// 
/// References: 
/// [1] - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.visiblemonthchanged.aspx
/// [2] - http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
/// </summary>
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // How to attach visibleMonthChanged event is explained in [1]
        someCalendar.VisibleMonthChanged +=
            new MonthChangedEventHandler(this.theVisibleMonthChanged);
    }

    protected void theVisibleMonthChanged(Object sender, MonthChangedEventArgs e) 
    {
        DateTime currentDate = DateTime.Now;
        DateTime dateOfMonthToDisable = currentDate.AddMonths(-1);
        if (e.NewDate.Month == dateOfMonthToDisable.Month)
        {
            someCalendar.VisibleDate = e.PreviousDate;
            // Custom date formats are explained in [2] 
            Page.ClientScript.RegisterClientScriptBlock(
                this.GetType(), 
                "someScriptKey",
                "<script>" + 
                "alert(\"Can not go before today's month of " +
                currentDate.ToString("MMM-yyyy") +
                ".\");" + 
                "</script>"
            );
        }
    }
}