如何设置asp.net日历以仅显示特定的日期范围

时间:2015-02-12 21:42:18

标签: c# sql asp.net date

我正在开发一个etime应用程序,其中包含paygroup的下拉列表,并根据所选的薪资组,填写该薪资组中的员工下拉列表,然后记录他们有交易的日期并执行这些日期的最小值和最大值并填充AvailableDateRange MIN-MAX。我的问题是我希望日历只显示从日期到日期的最小和最大日期范围,任何大于最大值的东西和任何小于最小值的东西都应该显示为灰色。任何帮助将不胜感激

enter image description here

default.aspx代码

 <asp:Label ID="Label1" runat="server" Text="PayGroup"></asp:Label>
       <asp:DropDownList ID="DropDownList1" runat="server" Height="25px" 
        Width="190px" AutoPostBack="True" DataSourceID="SqlDataSource1" 
        DataTextField="Paygroup" DataValueField="Paygroup" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:EtimeHistoryConnectionString %>" 
        SelectCommand="SELECT Paygroup FROM dbo.EtimeEmployees GROUP BY Paygroup">
    </asp:SqlDataSource>
        <asp:Label ID="Label2" runat="server" Text="Employee"></asp:Label>
    <asp:DropDownList ID="DropDownList2" runat="server" Height="25px" Width="428px" 
        AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="employee" 
        DataValueField="empid" 
        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:EtimeHistoryConnectionString %>" 
        SelectCommand="SELECT Paygroup, EtimeEmpID AS empid, EmpName + ' - ' + EtimeEmpID AS employee FROM EtimeEmployees WHERE (Paygroup = @paygroup) ORDER BY EmpName">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="paygroup" 
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
        ConnectionString="<%$ ConnectionStrings:EtimeHistoryConnectionString %>" 
        SelectCommand="SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList2" Name="empid" 
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <p>
        <asp:Label ID="Label3" runat="server" Text="AvailableDateRange MIN-MAX"></asp:Label>
    <asp:DropDownList ID="DropDownList3" runat="server" 
        DataSourceID="SqlDataSource3" DataTextField="mindate" 
        DataValueField="mindate" AutoPostBack="True" 
        onselectedindexchanged="DropDownList3_SelectedIndexChanged">
    </asp:DropDownList>
        <asp:Label ID="Label6" runat="server" Text="-&gt;"></asp:Label>
    <asp:DropDownList ID="DropDownList4" runat="server" 
        DataSourceID="SqlDataSource3" DataTextField="maxdate" 
        DataValueField="maxdate" AutoPostBack="True" 
        onselectedindexchanged="DropDownList4_SelectedIndexChanged">
    </asp:DropDownList>
    </p>
    <p>
        <asp:Label ID="Label4" runat="server" Text="From Date" ForeColor="Red"></asp:Label>
        <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" 
            BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
            Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" 
            ondayrender="Calendar1_DayRender" 
            ShowGridLines="True" Width="945px" SelectionMode="None">
            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
            <SelectorStyle BackColor="#FFCC66" />
            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#CC9966" />
            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
                ForeColor="#FFFFCC" />
        </asp:Calendar>
        <asp:Label ID="Label5" runat="server" Text="To Date" ForeColor="Red"></asp:Label>
        <asp:Calendar ID="Calendar2" runat="server" BackColor="#FFFFCC" 
            BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
            Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" 
            ShowGridLines="True" Width="945px">
            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
            <SelectorStyle BackColor="#FFCC66" />
            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#CC9966" />
            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
                ForeColor="#FFFFCC" />
        </asp:Calendar>

我试过这个

 protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.Date == DateTime.Parse(DropDownList3.Text))
            {
                e.Day.IsSelectable = true;
            }
            if (e.Day.Date < DateTime.Parse(DropDownList3.Text))
            {
                e.Day.IsSelectable = false;
            }
        }

1 个答案:

答案 0 :(得分:0)

您可以通过处理DayRender事件来实现:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.Date > DateTime.Now)    // use any selection criteria
    {
        // you can do all kinds of stuff here
        e.Cell.Enabled = false;
        e.Cell.ForeColor = Color.DarkGray;
    }
}