Rendering a calendar control

  • Thread starter Thread starter Winshent
  • Start date Start date
W

Winshent

I am still learning ASP.net and am having a problem with rendering a
calendar control.

I initially had the control on the same page as the button which
triggered it to make it visible. Now i have dumped its own page so it
pops up through javascript. The calendar is now not being rendered.

The calender is being rendered so that all days with existing records
are disabled.

Any ideas? Here is the code from the aspx and aspx.vb files

Thanks

Vincent

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="frmCalendar.aspx.vb" Inherits="SPC.frmCalendar"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Calendar</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:calendar id="myCalendar" style="Z-INDEX: 141; LEFT: 8px;
POSITION: absolute; TOP: 8px" runat="server"
CellSpacing="1" Height="120px" Width="200px" Font-Size="X-Small"
Font-Names="Verdana" BorderColor="Blue"
BorderStyle="Inset">
<TodayDayStyle BackColor="#FFC0C0"></TodayDayStyle>
<DayStyle Font-Size="XX-Small" BorderWidth="1px"
BorderStyle="None" BackColor="White"></DayStyle>
<NextPrevStyle ForeColor="RoyalBlue"
BackColor="White"></NextPrevStyle>
<DayHeaderStyle Font-Size="XX-Small" Font-Names="Verdana"
BorderWidth="1px" ForeColor="White" BorderColor="Black"
BackColor="RoyalBlue"></DayHeaderStyle>
<SelectedDayStyle BorderColor="Black"
BackColor="RoyalBlue"></SelectedDayStyle>
<TitleStyle Font-Size="XX-Small" ForeColor="RoyalBlue"
BackColor="White"></TitleStyle>
<OtherMonthDayStyle Font-Size="XX-Small" Font-Names="Verdana"
Font-Strikeout="True" BackColor="Silver"></OtherMonthDayStyle>
</asp:calendar></form>
</body>
</HTML>

===========================================================================

Imports System.Web.ui

Public Class frmCalendar
Inherits System.Web.UI.Page

Private dsGroupDates As New DataSet
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.

Protected WithEvents myDataAdapter As
System.Data.SqlClient.SqlDataAdapter
Protected WithEvents SelectGroupedDatesCmd As
System.Data.SqlClient.SqlCommand
Protected WithEvents myConnection As
System.Data.SqlClient.SqlConnection
Protected WithEvents myCalendar As
System.Web.UI.WebControls.Calendar

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
InitializeStuff()
myDataAdapter.Fill(dsGroupDates, "Results")
End Sub

Public Sub OpenConn()
myConnection = New
System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString"))
myConnection.Open()
End Sub

Public Sub CloseConn()
myConnection.Close()
End Sub

Private Sub InitializeStuff()
myDataAdapter = New System.Data.SqlClient.SqlDataAdapter
OpenConn()
SelectGroupedDatesCmd = New System.Data.SqlClient.SqlCommand

myDataAdapter.SelectCommand = SelectGroupedDatesCmd

SelectGroupedDatesCmd.CommandText = "SELECT LogDate FROM
tblResults GROUP BY LogDate ORDER BY LogDate"
SelectGroupedDatesCmd.Connection = myConnection
CloseConn()

End Sub

Private Sub Calendar_DayRender(ByVal sender As System.Object, ByVal
e As System.Web.UI.WebControls.DayRenderEventArgs)
Dim DateExists As Date
Dim i As Integer
For i = 0 To GetExistingDates.Count - 1
DateExists = GetExistingDates(i)("LogDate")
If (e.Day.Date = DateExists) Then
e.Day.IsSelectable = False
End If
Next
End Sub

Private Function GetExistingDates() As DataRowCollection
'Collection

myDataAdapter.SelectCommand = SelectGroupedDatesCmd

Dim RowsColl As DataRowCollection
Dim dView As DataView

RowsColl = dsGroupDates.Tables("Results").Rows
Return RowsColl
'dView = dsGroupDates.Tables("Results").DefaultView

End Function

End Class
 
First up, I'd clean up your code a bit by thinning it out. You don't need
an explicit Command object because the DataAdapter has all the necessary
command objects built into it. Also, I would set up my connection object in
a less formal way than you have. There is no need to explicitly open and
close your connection object because when using a DataAdapter, it will do
this for you. There is also no need to create an explicit rows collection,
just use the rows exposed by the DataSet's table.

As for the calendar control itself, put it back on the same page as the
button. Set the calendar's "Visible" property to "False" in the properties
dialog window and then in the button's click event handler, simply set the
calendar's visible property to true.

Here's what I would write:

Public Class frmCalemdar
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

Dim myConnection as System.Data.SQLClient.SQLConnection
Dim myDataAdapter as System.Data.SQLClient.DataAdapter
Dim dsGroupDates As New DataSet

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Dim sqlStr as String = "SELECT LogDate FROM tblResults GROUP BY
LogDate ORDER BY LogDate"
Dim conStr as String =
System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString
")

myConnection = New System.Data.SqlClient.SqlConnection(conStr)
myDataAdapter = New System.Data.SQLClient.DataAdapter(sqlStr,
myConnection)
Try
myDataAdapter.Fill(dsGroupDates, "Results")
Catch ex as Exception
'handle any exceptions here
End Try

'Store the DataSet, so that we don't need to connect to the DB on
every page call
Cache.Insert("theData", dsGroupDates)
Else
'Retrieve the data stored in the cache
dsGrouptDates = CType(cache.item("theData"), DataSet)
End If
End Sub

Private Sub Calendar_DayRender(ByVal sender As System.Object, ByVal e As
System.Web.UI.WebControls.DayRenderEventArgs)
Dim DateExists As Date
Dim i As Integer
For i = 0 To dsGroupDates.Tables("Results").Rows.Count - 1
DateExists = dsGroupDates.Tables("Results").Rows(i)("LogDate")
If (e.Day.Date = DateExists) Then
e.Day.IsSelectable = False
End If
Next
End Sub
End Class

Good luck!
 
Scott

Thanks for your response. I have now sorted the problem. I copied and
pasted the code from my original page to my calendar page. For some
reason the code had become 'unwired' from the page. I simply deleted the
code, saved the module and then pasted it back in again. Have had this
problem with Access 2000, am surprised to have it with VS.

Anyway, thanks for your code tips.
 
Back
Top