Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calendar - extra week displayed 1

Status
Not open for further replies.

ShikkurDude

Programmer
Mar 15, 2005
55
US
My Calendar control always displays 6 weeks per month. I understand one extra is for the partial week that's in this month and part of it's in the other (prior or next) month, but there's always yet another week that's totally in the next (or previous) month. How do I get rid of that completely extra week?

Thanks,
E.
 
Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
e.Cell.Controls.Add(New LiteralControl("<br>Holiday"))
If e.Day.Date.DayOfWeek = DayOfWeek.Sunday Then
If e.Day.Date.Month <> CType(sender, Calendar).VisibleDate.Month And _
e.Day.Date.AddDays(6).Month <> CType(sender, Calendar).VisibleDate.Month Then
Dim lit As New Literal
lit.Text = "<script id='" & e.Day.Date.Ticks.ToString & "'>document.getElementById('" & e.Day.Date.Ticks.ToString & "').parentNode.parentNode.style.display='none';</script>"
e.Cell.Controls.Add(lit)
End If
End If

End Sub
 
Err...that doesn't appear to work correctly. For example, when the page first loads the calendar has no rows apart from the day names (for the month of may). Also, september of this year for example, doesn't have the last week.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
By the way, an easy way would be just to remove all days that are not in the current month that the user is viewing. e.g.
Code:
    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If e.Day.IsOtherMonth Then e.Cell.Controls.RemoveAt(0)
    End Sub


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
rTomes, thanks very much.

Ca8msm, thanks for catching that before I even read it :) What your saying would be easier - but I do want those days on the calendar - just not the entirely extra week. Any (more) ideas?

Thanks!
E.
 
Yes, going back to the current cell ID idea, it could be adapted to do something like:
Code:
Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender

        Select Case CurrentCellID
            Case 1
                If e.Day.IsOtherMonth = True And e.Day.Date.AddDays(7).Day = 1 Then
                    blnRemoveFirstRow = True
                    e.Cell.Controls.RemoveAt(0)
                End If

            Case 2 To 7
                If blnRemoveFirstRow = True Then
                    e.Cell.Controls.RemoveAt(0)
                End If

            Case 36
                If e.Day.IsOtherMonth = True Then
                    blnRemoveLastRow = True
                    e.Cell.Controls.RemoveAt(0)
                End If

            Case 37 To 42
                If blnRemoveLastRow = True Then
                    e.Cell.Controls.RemoveAt(0)
                End If

        End Select
        CurrentCellID += 1
    End Sub

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Ca8msm,

I tried your code - at least I think I tried your code... This is verbatim what I tried:
Code:
		Dim blnRemoveFirstRow As Boolean = False
		Dim blnRemoveLastRow As Boolean = False

		Select Case e.Cell.ID
			Case 1
				If e.Day.IsOtherMonth = True And e.Day.Date.AddDays(7).Day = 1 Then
					blnRemoveFirstRow = True
					e.Cell.Controls.RemoveAt(0)
				End If

			Case 2 To 7
				If blnRemoveFirstRow = True Then
					e.Cell.Controls.RemoveAt(0)
				End If

			Case 36
				If e.Day.IsOtherMonth = True Then
					blnRemoveLastRow = True
					e.Cell.Controls.RemoveAt(0)
				End If

			Case 37 To 42
				If blnRemoveLastRow = True Then
					e.Cell.Controls.RemoveAt(0)
				End If

		End Select
		e.Cell.ID += 1
...and it didn't do anything at all. It compiled and ran, but all 6 weeks still appeared for this month...

Did it work for you?

Thanks,
E.
 
Sorry, I missed out the global declarations. At the top of your page add:
Code:
    Dim CurrentCellID As Integer = 1
    Dim blnRemoveFirstRow As Boolean = False
    Dim blnRemoveLastRow As Boolean = False
and then add the code to the DayRender event:
Code:
Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender

        Select Case CurrentCellID
            Case 1
                If e.Day.IsOtherMonth = True And e.Day.Date.AddDays(7).Day = 1 Then
                    blnRemoveFirstRow = True
                    e.Cell.Controls.RemoveAt(0)
                End If

            Case 2 To 7
                If blnRemoveFirstRow = True Then
                    e.Cell.Controls.RemoveAt(0)
                End If

            Case 36
                If e.Day.IsOtherMonth = True Then
                    blnRemoveLastRow = True
                    e.Cell.Controls.RemoveAt(0)
                End If

            Case 37 To 42
                If blnRemoveLastRow = True Then
                    e.Cell.Controls.RemoveAt(0)
                End If

        End Select
        CurrentCellID += 1
    End Sub

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Hmmm, still doesn't work for me... It worked for you?

E.
 
Did you copy my last example as the code you tried used e.Cell.ID rather than CurrentCellID. The exact code above worked fine as far as my tests went.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Full disclosure: I have this:
Code:
	Private Sub DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles cldPHOEvents.DayRender

		RemoveExtraWeek(e)
...

And then this:
Code:
	Private Sub RemoveExtraWeek(ByRef e As System.Web.UI.WebControls.DayRenderEventArgs)

		Select Case CurrentCellID
			Case 1
				If e.Day.IsOtherMonth = True And e.Day.Date.AddDays(7).Day = 1 Then
					blnRemoveFirstRow = True
					e.Cell.Controls.RemoveAt(0)
				End If

			Case 2 To 7
				If blnRemoveFirstRow = True Then
					e.Cell.Controls.RemoveAt(0)
				End If

			Case 36
				If e.Day.IsOtherMonth = True Then
					blnRemoveLastRow = True
					e.Cell.Controls.RemoveAt(0)
				End If

			Case 37 To 42
				If blnRemoveLastRow = True Then
					e.Cell.Controls.RemoveAt(0)
				End If

		End Select
		CurrentCellID += 1

	End Sub

And it does not do anything...
E.
 
OK three questions as it is working perfectly so you must have something set incorrectly/differently to me:

1) What month are you looking at when you say it does nothing
2) What is the first day that is displayed on the calendar header row? i.e. what order are the days in
3) If you add "Dim s As String = Calendar1.FirstDayOfWeek" to the page load, what does s equal?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Also in number two I meant was it Mon, Tue, Wed or Sun, Mon, Tue etc



--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
If it turns out that it is Mon, Tue Wed...then you have exactly the same settings as me; the reason that it will not "do anything" for May 2005 is that the 1st of May falls on the Sunday of the first week. Therefore the first week should stay. Also, the 30th and 31st fall into the 6th week and therefore it should stay as well.

If you move to June 2005 then you will notice that the 6th week is removed as it contained the 4th to the 10th of the next month (July).

That make sense?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Umm, nope...

I would like that first week in May not to appear. I agree with you on the last week in May, though - it (appropriately doesn't appear). Secondly, for June 2005, it still shows 6 full weeks...

Hmmm...
E.
 
Not sure why you get an error, this works in IE, Firefox and Netscape. You can't turn off just table cells visibility because that will break the TR table row on the client side. I added a delay to this so incase the object has not rendered immediately. I accidentally had an example of another thread that I had in the previous code that should have not been there.
This does hide irrelevant weeks.

My ASPX page has:
<asp:Calendar id="Calendar1" runat="server"></asp:Calendar>

Page_Load has:
Me.Calendar1.VisibleDate = Now

Then Day_Render:

Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
("<br>Holiday"))
If e.Day.Date.DayOfWeek = DayOfWeek.Sunday Then
If e.Day.Date.Month <> CType(sender, Calendar).VisibleDate.Month And _
e.Day.Date.AddDays(6).Month <> CType(sender, Calendar).VisibleDate.Month Then
Dim lit As New Literal
lit.Text = "<script id='" & e.Day.Date.Ticks.ToString & "'>window.setTimeout('document.getElementById(\'" & e.Day.Date.Ticks.ToString & "\').parentNode.parentNode.style.display=\'none\'',50);</script>"
e.Cell.Controls.Add(lit)
End If
End If

End Sub
 
I would like that first week in May not to appear.
Why? The 1st falls on the Sunday so if you don't show the row then you won't see the first of the month.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
On my calendar, the first week is Sunday, April 24th - Saturday, April 30th. That week shouldn't appear at all.

The last week is Sunday, May 29th - Saturday, June 4th. That one is ok.

Are we showing the same page?
E.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top