1

我有两个日历作为SqlDataSource;的控制参数。fromDateCaltoDateCal。两个日历都有各自的DayRender事件,这些事件限制了每个日历的可用日期。

日历设置如下:

<asp:Calendar ID="fromDateCal" runat="server" BackColor="White" BorderColor="#999999"
        CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
        ForeColor="Black" Height="180px" Width="200px" onDayRender="fromDateCal_DayRender">
    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
    <NextPrevStyle VerticalAlign="Bottom" />
    <OtherMonthDayStyle ForeColor="#808080" />
    <SelectedDayStyle BackColor="#CEDEF4" BorderColor="#7BA7E1" BorderWidth="2" BorderStyle="Solid" Font-Bold="True" ForeColor="#2966B8" />
    <SelectorStyle BackColor="#FFCC99" />
    <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
    <WeekendDayStyle BackColor="#FFFFCC" />
</asp:Calendar>

<asp:Calendar ID="toDateCal" runat="server" BackColor="White" BorderColor="#999999"
        CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
        ForeColor="Black" Height="180px" Width="200px" OnDayRender="fromDateCal_DayRender">
    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
    <NextPrevStyle VerticalAlign="Bottom" />
    <OtherMonthDayStyle ForeColor="#808080" />
    <SelectedDayStyle BackColor="#CEDEF4" BorderColor="#7BA7E1" BorderWidth="2" BorderStyle="Solid" Font-Bold="True" ForeColor="#2966B8" />
    <SelectorStyle BackColor="#FFCC99" />
    <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
    <WeekendDayStyle BackColor="#FFFFCC" />
</asp:Calendar>

相应的 DayRender 事件是:

protected void fromDateCal_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
    if (e.Day.Date > toDateCal.SelectedDate.AddDays(-1))
    {
        e.Day.IsSelectable = false;
        e.Cell.CssClass = "disabledDate";
    }
}

protected void toDateCal_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
    if (e.Day.Date > DateTime.Now || e.Day.Date < fromDateCal.SelectedDate.AddDays(1))
    {
        e.Day.IsSelectable = false;
        e.Cell.CssClass = "disabledDate";
    }
}

设置的限制是;

  • fromDateCal不能大于或等于toDateCal.SelectedDate
  • toDateCal不能大于DateTime.Now也不能小于或等于fromDateCal.SelectedDate

当页面加载时,这些限制就会被应用,一切都会出现并且运行良好。但是,当在任一日历上选择日期时,DayRender事件不会立即执行。我发现您必须从同一个日历中再次选择相同的日期才能更新两者的限制。

有什么办法可以解决这个问题并DayRender首先执行PostBack?我已经用尽了我对 ASP.Net 的有限知识,现在正在寻求帮助。

谢谢。

4

0 回答 0