[英]Disable previous month link in Calendar control
我正在尝试找到一种方法来禁用“ Calendar
控件中的“上个月”链接。 看来这应该是一个非常简单的任务,但是我无法在文档中找到任何属性或方法来执行此操作。
请注意,我正在寻找一种仅禁用上个月链接而不禁用两个链接的方法。 仅禁用“下个月”链接应该是相同的解决方案,因此也可以接受。 还请注意, ShowNextPrevMonth
属性是不可行的解决方案,因为它用于隐藏,这是可以接受的,但不太理想,它隐藏了两个链接而不是一个链接。
谢谢你的帮助! 编码愉快!
旁注:我不是在寻找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.