简体   繁体   English

在“日历”控件中禁用上个月的链接

[英]Disable previous month link in Calendar control

I am trying to find a way to disable the "Previous Month" link in my Calendar control. 我正在尝试找到一种方法来禁用“ Calendar控件中的“上个月”链接。 This seems like it should be a very simple task, yet I have not been able to find any properties or methods to do this, in the documentation. 看来这应该是一个非常简单的任务,但是我无法在文档中找到任何属性或方法来执行此操作。

Please note that I am looking for a way to disable only the Previous Month link, and not both links. 请注意,我正在寻找一种禁用上个月链接而不禁用两个链接的方法。 Disabling just the "Next Month" link should be the same solution, so that would acceptable too. 仅禁用“下个月”链接应该是相同的解决方案,因此也可以接受。 Please also note that the ShowNextPrevMonth property is an unfeasible solution, as it is used for hiding, which would be acceptable but less desirable, and hides both links, rather than just one. 还请注意, ShowNextPrevMonth属性是不可行的解决方案,因为它用于隐藏,这是可以接受的,但不太理想,它隐藏了两个链接而不是一个链接。

.NET Calendar Control Documentation .NET日历控件文档


Thanks for your help! 谢谢你的帮助! Happy coding! 编码愉快!


Side-note: I am not looking for JavaScript solutions, as I can devise one fairly simply. 旁注:我不是在寻找JavaScript解决方案,因为我可以相当简单地设计一个。 However, if anyone does happen to know how I can apply a different CssClass to each of the links, I would up-vote that solution. 但是,如果有人碰巧知道如何将不同的CssClass应用于每个链接,我会投票赞成该解决方案。

   <%@ 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>"
            );
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM