簡體   English   中英

在“日歷”控件中禁用上個月的鏈接

[英]Disable previous month link in Calendar control

我正在嘗試找到一種方法來禁用“ Calendar控件中的“上個月”鏈接。 看來這應該是一個非常簡單的任務,但是我無法在文檔中找到任何屬性或方法來執行此操作。

請注意,我正在尋找一種禁用上個月鏈接而不禁用兩個鏈接的方法。 僅禁用“下個月”鏈接應該是相同的解決方案,因此也可以接受。 還請注意, ShowNextPrevMonth屬性是不可行的解決方案,因為它用於隱藏,這是可以接受的,但不太理想,它隱藏了兩個鏈接而不是一個鏈接。

.NET日歷控件文檔


謝謝你的幫助! 編碼愉快!


旁注:我不是在尋找JavaScript解決方案,因為我可以相當簡單地設計一個。 但是,如果有人碰巧知道如何將不同的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