Calendar Control - Programatically set the calendar to a date range

S

Shevek

Hi All, Hope someone can help!

I am building an Event Calendar app based around the Calendar
WebControl which builds an SQL string based on the SelectedDates
property which is passed to Page.StartDate and Page.EndDate
properties.

This works nicely and I can select a Day, Week or Month and run my SQL
against them.

There is also a shortcuts dropdown to allow the user to select Today,
Last Month, This Month, Next Month, Next Two Weeks, Next 30 Days, Next
60 Days, Next 90 Days.

Again, these all work nicely as I set the Page.StartDate and
Page.EndDate based on the option and run the SQL then update the
Calendar.VisibleDate to Page.StartDate.

The problem is it would be nice for the Calendar object to reflect the
shortcut ranges. This is OK for Today as I can just set the
SelectedDate to the Page.StartDate. However the SelectedDates property
is read only.

Is there any way to set a range of dates to be selected based on the
Page.StartDate and Page.EndDate properties?

Many TIA

--


Shevek

The Poster Previously Known As Moldy
 
A

avnrao

you can write a handler for DayRender event and check for the date and
change color accordingly.

private void Calendar1_DayRender(object
sender,System.Web.UI.WebControls.DayRenderEventArgs e )
{
if (e.Day.Date >= Page.StartDate && e.Day.Date <= Page.EndDate)
{
e.Cell.BackColor = System.Drawing.Color.LightGray;
}
}

hth,
Av.
 
S

Shevek

you can write a handler for DayRender event and check for the date and
change color accordingly.

private void Calendar1_DayRender(object
sender,System.Web.UI.WebControls.DayRenderEventArgs e )
{
if (e.Day.Date >= Page.StartDate && e.Day.Date <= Page.EndDate)
{
e.Cell.BackColor = System.Drawing.Color.LightGray;
}
}

hth,

Doesn't quite work!

I already use the DayRender event to apply styles and onmouseover to
give the dates borders and rollovers.

If I change the first line to

If (e.Day.Date.ToShortDateString >= Me.StartDate And
e.Day.Date.ToShortDateString <= Me.EndDate) or e.Day.IsSelected Then

then it seems to keep the cssclass in the viewstate and is there when
I browse back and forth through the calendar....


If e.Day.IsSelected Then
If bEvent Then
e.Cell.ApplyStyle(ForeOrange)
e.Cell.CssClass = "EventSelectedDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventSelectedDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventSelectedDay';")
Else
e.Cell.CssClass = "SelectedDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'SelectedDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'SelectedDay';")
End If
ElseIf e.Day.IsWeekend Then
If bEvent Then
e.Cell.ApplyStyle(ForeOrange)
e.Cell.CssClass = "EventWeekendDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventWeekendDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventWeekendDay';")
Else
e.Cell.CssClass = "WeekendDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'WeekendDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'WeekendDay';")
End If
ElseIf e.Day.IsOtherMonth Then
If bEvent Then
e.Cell.ApplyStyle(ForeYellow)
e.Cell.CssClass = "EventOtherMonthDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventOtherMonthDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventOtherMonthDay';")
Else
e.Cell.CssClass = "OtherMonthDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'OtherMonthDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'OtherMonthDay';")
End If
Else
If bEvent Then
e.Cell.ApplyStyle(ForeOrange)
e.Cell.CssClass = "EventDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventDay';")
Else
e.Cell.CssClass = "Day"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'DayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'Day';")
End If
End If


--


Shevek

The Poster Previously Known As Moldy
 
S

Shevek

Doesn't quite work!

I already use the DayRender event to apply styles and onmouseover to
give the dates borders and rollovers.

If I change the first line to

If (e.Day.Date.ToShortDateString >= Me.StartDate And
e.Day.Date.ToShortDateString <= Me.EndDate) or e.Day.IsSelected Then

then it seems to keep the cssclass in the viewstate and is there when
I browse back and forth through the calendar....

Got it! It was not working because my Page.StartDate and Page.EndDate
are both dates stored as strings...

Dim dStartDate, dEndDate As Date
If IsDate(Me.StartDate) Then dStartDate = Me.StartDate
If IsDate(Me.EndDate) Then dEndDate = Me.EndDate
If (e.Day.Date >= dStartDate And e.Day.Date <= dEndDate) Then

....has fixed it!

Cheers!
If e.Day.IsSelected Then
If bEvent Then
e.Cell.ApplyStyle(ForeOrange)
e.Cell.CssClass = "EventSelectedDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventSelectedDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventSelectedDay';")
Else
e.Cell.CssClass = "SelectedDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'SelectedDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'SelectedDay';")
End If
ElseIf e.Day.IsWeekend Then
If bEvent Then
e.Cell.ApplyStyle(ForeOrange)
e.Cell.CssClass = "EventWeekendDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventWeekendDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventWeekendDay';")
Else
e.Cell.CssClass = "WeekendDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'WeekendDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'WeekendDay';")
End If
ElseIf e.Day.IsOtherMonth Then
If bEvent Then
e.Cell.ApplyStyle(ForeYellow)
e.Cell.CssClass = "EventOtherMonthDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventOtherMonthDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventOtherMonthDay';")
Else
e.Cell.CssClass = "OtherMonthDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'OtherMonthDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'OtherMonthDay';")
End If
Else
If bEvent Then
e.Cell.ApplyStyle(ForeOrange)
e.Cell.CssClass = "EventDay"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'EventDayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'EventDay';")
Else
e.Cell.CssClass = "Day"
e.Cell.Attributes.Add("onmouseover",
"JavaScript:this.className = 'DayOver';")
e.Cell.Attributes.Add("onmouseout",
"JavaScript:this.className = 'Day';")
End If
End If


--


Shevek

The Poster Previously Known As Moldy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top