ASP.net日历控件有条件

时间:2011-10-31 03:02:33

标签: asp.net

我希望有一个日历控件,只允许用户选择将来工作日(周一至周五)的日期,以及是否在例如之后。当天上午9点,这个日期也将无法使用,然后将其返回到文本框中。

怎样才能轻松做到这一点。我还是ASP.net的新手。

谢谢,

3 个答案:

答案 0 :(得分:1)

处理DayRender事件:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.IsWeekend == true)
    {
        e.Cell.Enabled = false;
        e.Cell.BackColor = Color.Gray;
    }

    // add your other criteria as needed
}

答案 1 :(得分:1)

<强>更新

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsWeekend == true)
        {
            e.Cell.Enabled = false;
            e.Day.IsSelectable = false;
            e.Cell.BackColor = Color.Gray;
        }
        else
        {
            e.Cell.Enabled = true;

            if (e.Day.Date == DateTime.Today)
            {
                // DateTime d = DateTime.Today;
                if (DateTime.Now.Hour > 9)
                {
                    e.Day.IsSelectable = false;
                    e.Cell.Enabled = false;
                    e.Cell.BackColor = Color.Gray;
                }
            }
            else if (e.Day.Date < DateTime.Today)
            {
                e.Day.IsSelectable = false;
                e.Cell.Enabled = false;
                e.Cell.BackColor = Color.Gray;
            }

        }

    }

<强>更新

<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" onselectionchanged="Calendar1_SelectionChanged" 
            ></asp:Calendar>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>




protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            TextBox1.Text = Calendar1.SelectedDate.ToString();
        }

答案 2 :(得分:1)

对于VB上的asp.net,你可以使用:

 Protected Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs)
            If (e.Day.Date.DayOfWeek = DayOfWeek.Friday) Then
                e.Day.IsSelectable = False
                e.Cell.ForeColor = Drawing.Color.Red
            End If
            If e.Day.Date < System.DateTime.Today Then
                e.Day.IsSelectable = False
            End If

 End Sub

这将禁用今天之前的日期并禁用周末选择。