【问题标题】:Disable previous month link in Calendar control在日历控件中禁用上个月链接
【发布时间】:2014-03-07 14:24:11
【问题描述】:

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

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

.NET Calendar Control Documentation


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


旁注:我不是在寻找 JavaScript 解决方案,因为我可以相当简单地设计一个。但是,如果有人碰巧知道我如何为每个链接应用不同的CssClass,我会对该解决方案投赞成票。

【问题讨论】:

  • 我假设您的意思是 ASP.NET Web 控件? (如果您可以链接到您正在使用的确切控件,这将有所帮助,以避免任何歧义的可能性。)
  • 如果启用了“下个月”链接,是否希望在用户移动到下个月时重新启用“上个月”链接?
  • 抱歉,@JonSkeet。我已按要求添加了链接:)
  • @Bolu - 这是计划,但我应该能够弄清楚如何做到这一点,只要我能弄清楚如何禁用“下一步”链接,最初。不过,我不会拒绝有关如何重新启用它的免费信息;)
  • 我认为您可以处理OnVisibleMonthChanged 事件?如果它移到上个月,就像什么都不做......这是一个example

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


【解决方案1】:
   <%@ 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>"
            );
        }
    }
}

【讨论】:

  • 此解决方案提醒访问者单击下一个/上一个链接将显示禁用月份,这与隐藏一个链接或另一个不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
相关资源
最近更新 更多