2014-03-07 24 views
0

我試圖找到一種方法來禁用我的Calendar控件中的「上個月」鏈接。這看起來應該是一個非常簡單的任務,但我還沒有找到任何屬性或方法來做到這一點,在文檔中。在日曆控件中禁用上個月的鏈接

請注意,我正在尋找一種方法來禁用只有上個月鏈接,而不是兩個鏈接。只禁用「下個月」鏈接應該是相同的解決方案,這樣也可以接受。另請注意,ShowNextPrevMonth屬性是一個不可行的解決方案,因爲它用於隱藏,這是可以接受的,但不太理想,並且隱藏了這兩個鏈接,而不僅僅是一個。

.NET Calendar Control Documentation


感謝您的幫助!快樂的編碼!


側面說明:我不是在尋找JavaScript的解決方案,我可以設計一個相當簡單。但是,如果有人確實知道我可以在每個鏈接上應用不同的CssClass,我會爲該解決方案投票。

+0

我假設你的意思是一個ASP.NET Web控件? (如果您可以鏈接到您正在使用的確切控件,這將有所幫助,只是爲了避免任何可能的歧義。) –

+0

如果啓用「下個月」鏈接,是否希望「上個月」鏈接在重新啓用時用戶移動到下個月? – Bolu

+0

對不起,@JonSkeet。我已經添加了鏈接,按照要求:) –

回答

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>" 
      ); 
     } 
    } 
} 
+0

此解決方案提醒訪問者點擊下一個/上一個鏈接會顯示禁用的月份,這與隱藏一個鏈接或另一個鏈接不同。 –