HL8
HL8

Reputation: 1419

ASP.net Calendar Control conditional

I want to have a calendar control to only allow users to select dates which are weekdays (Mon-Fri) in the future and if it's after eg. 9.00 am of the current day then this date will also not be available, this to be then returned into a text box.

How can a do this easily. I'm still very new to ASP.net.

Thank you,

Upvotes: 1

Views: 1958

Answers (3)

Bipul Roy
Bipul Roy

Reputation: 163

for asp.net on VB you can use this :

 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

This will disable the dates before today and disable selection of weekend.

Upvotes: 0

Arun Rana
Arun Rana

Reputation: 8606

Updated:

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;
            }

        }

    }

Update:

<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();
        }

Upvotes: 1

Steve Wellens
Steve Wellens

Reputation: 20640

Handle the DayRender event:

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
}

Upvotes: 1

Related Questions