QPR
asked on
Page Load not firing?
Firstly I should say that this isn't my code so bare with me.....
I d/l it from here if you need to see the original (zip file at foot of page)
http://aspnet.4guysfromrolla.com/articles/041603-1.2.aspx
I split the code and put the vb into the code behind file.
It has this sub...
Public Sub Page_Load()
tempDate = Me.Calendar1.TodaysDate
If Not (Page.IsPostBack) Then
Get_DBItems()
End If
End Sub
and in Get_DBItems it errors on this line....
parameterDate.Value = tempDate.AddDays(-40) saying the result is not a date.
When I check the initial value of parameterDate.Value is "nothing"
Strange thing is that I put a breakpoint in the Page_Load sub and it never seems to get there, when I launch from VS2005 and goes straight to the breakpoint within Get_DBItems.
I'll post my aspx and aspx.vb below.. any ideas?
The aspx is inside a <asp:content>
<div id="daydetail" runat="server" visible="false">
<h2>Details for <span id="selectedday" runat="server" /></h2>
<span id="daydetail_render" runat="server" />
</div>
<div runat="server">
<asp:Calendar id="Calendar1" OnDayRender="Calendar1_Day Render"
OnSelectionChanged="Calend ar1_Select ionChanged "
OnVisibleMonthChanged="Mon thChanged"
DayStyle-Height="100" DayStyle-Width="75" DayStyle-HorizontalAlign=" Left"
DayStyle-verticalalign="To p"
NextPrevFormat="FullMonth" SelectionMode="Day"
TitleStyle-Font-Bold="Fals e"
TitleStyle-Font-Size="12" BackColor="white" BorderColor="#000000"
CellPadding="2" CellSpacing="2" Runat="server"
SelectedDayStyle-BackColor ="#faebd7"
SelectedDayStyle-ForeColor ="#000000"
OtherMonthDayStyle-ForeCol or="#C0C0C 0" DayStyle-BorderStyle="Soli d"
DayStyle-BorderWidth="1" TodayDayStyle-ForeColor="B lack" Height="600"
Width="750">
</asp:Calendar><br><br>
</div>
then the code behind.....
Imports System.Data
Imports System.Data.SqlClient
Partial Class HR_Calendar_Default
Inherits System.Web.UI.Page
Public Structure MyDates
Dim _Title As String
Dim _Date As DateTime
End Structure
Public MyCollection As Collection
Public tempDate As DateTime
Public Sub Page_Load()
tempDate = Me.Calendar1.TodaysDate
If Not (Page.IsPostBack) Then
Get_DBItems()
End If
End Sub
Public Sub Get_DBItems()
Dim myCommand As SqlCommand
Dim ConnectionString As String = System.Configuration.Confi gurationMa nager.Conn ectionStri ngs("Intra netConnect ionString" ).Connecti onString
Dim myConnection As System.Data.SqlClient.SqlC onnection = New System.Data.SqlClient.SqlC onnection( Connection String)
myCommand = New SqlCommand("GetHRCalendarI tems", myConnection)
myCommand.CommandType = CommandType.StoredProcedur e
'// Set to 40 days before the current value of tempDate
Dim parameterDate As New SqlParameter("@_Date", SqlDbType.DateTime)
parameterDate.Value = tempDate.AddDays(-40)
myCommand.Parameters.Add(p arameterDa te)
'// Set to 40 days after the current value of tempDate
Dim parameterDate2 As New SqlParameter("@_Date_Plus_ 1", SqlDbType.DateTime)
parameterDate2.Value = tempDate.AddDays(40)
myCommand.Parameters.Add(p arameterDa te2)
Dim myDataReader As SqlDataReader
myConnection.Open()
myDataReader = myCommand.ExecuteReader()
Dim temp As MyDates
MyCollection = New Collection()
While (myDataReader.Read())
'//Created each time through the loop to get a new item to load and add to the collection
temp = New MyDates()
temp._Title = myDataReader.Item("Title")
temp._Date = CDate(myDataReader.Item("_ Date"))
MyCollection.Add(temp)
End While
'// Close
myDataReader.Close()
myConnection.Close()
End Sub
Public Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
Dim Item As MyDates '//Temporary storage for looping through the collection
Dim DayHold As DateTime = "01/01/1900" '//Temporary date to check for multiple items per day
Dim MultipleItemDay As Boolean = False
'// Set to true if the date is in the collection and needs to be displayed
Dim DayTextHasChanged As Boolean = False
'//To build the string for the contents of the table cell
'//I'll use the stingbuilder class instead of concatenation
Dim temp As StringBuilder
If IsNothing(MyCollection) = True Then
Get_DBItems()
End If
For Each Item In MyCollection
'//The collection is loaded in date order. So, if the date is different than the DayHold variable
'//And I've already found the right day in the collection, I can exit the for loop
If DayHold <> Item._Date Then
If DayTextHasChanged = True Then
Exit For
End If
MultipleItemDay = False
DayHold = Item._Date
Else
MultipleItemDay = True
End If
'//If I've found a date matching the current date...I need to create the appropriate text
If e.Day.Date = Item._Date.ToString("d") Then
If MultipleItemDay = False Then
temp = New StringBuilder() '//Create a new stringbuilder object
Else
temp.Append("<br>") '//add a seperator to the stringbuilder
End If
temp.Append(Item._Title.To String())
temp.Append("</span>")
DayTextHasChanged = True '//Set the flag
End If
Next
'//If there was an Item for this day, add the stringbuilder text to the table cell
If DayTextHasChanged = True Then
e.Cell.Controls.Add(New LiteralControl(temp.ToStri ng()))
End If
End Sub
Public Sub Calendar1_SelectionChanged (ByVal sender As Object, ByVal e As EventArgs)
Dim ConnectionString As String = System.Configuration.Confi gurationMa nager.Conn ectionStri ngs("Intra netConnect ionString" ).Connecti onString
Dim myConnection As System.Data.SqlClient.SqlC onnection = New System.Data.SqlClient.SqlC onnection( Connection String)
Dim myCommand As SqlCommand
Dim myDataReader As SqlDataReader
myCommand = New SqlCommand("GetHRCalendarS ingleItem" , myConnection)
myCommand.CommandType = CommandType.StoredProcedur e
Dim parameter_Date As New SqlParameter("@_Date", SqlDbType.DateTime, 8)
parameter_Date.Value = Calendar1.SelectedDate
myCommand.Parameters.Add(p arameter_D ate)
myConnection.Open()
myDataReader = myCommand.ExecuteReader()
Dim temp As String = ""
Dim Category As String = ""
While (myDataReader.Read())
temp &= "Title : <a href=""edit.aspx?ID=" & myDataReader.Item("ID") & """>" & myDataReader.Item("Title") & "</a><br>Date : " & myDataReader.Item("_Date") & "<br><br>"
End While
daydetail.Visible = True
daydetail_render.InnerHTML = temp
myDataReader.Close()
myConnection.Close()
selectedday.InnerHTML = Calendar1.SelectedDate.ToS tring("d MMM, yyyy")
tempDate = Calendar1.SelectedDate '//
Get_DBItems()
End Sub
Public Sub MonthChanged(ByVal source As Object, ByVal e As MonthChangedEventArgs)
'//Set the tempDatevariable to the value in the MonthChangedEventArgs NewDate property
tempDate = e.NewDate
'//Reload the collection
Get_DBItems()
End Sub
End Class
I d/l it from here if you need to see the original (zip file at foot of page)
http://aspnet.4guysfromrolla.com/articles/041603-1.2.aspx
I split the code and put the vb into the code behind file.
It has this sub...
Public Sub Page_Load()
tempDate = Me.Calendar1.TodaysDate
If Not (Page.IsPostBack) Then
Get_DBItems()
End If
End Sub
and in Get_DBItems it errors on this line....
parameterDate.Value = tempDate.AddDays(-40) saying the result is not a date.
When I check the initial value of parameterDate.Value is "nothing"
Strange thing is that I put a breakpoint in the Page_Load sub and it never seems to get there, when I launch from VS2005 and goes straight to the breakpoint within Get_DBItems.
I'll post my aspx and aspx.vb below.. any ideas?
The aspx is inside a <asp:content>
<div id="daydetail" runat="server" visible="false">
<h2>Details for <span id="selectedday" runat="server" /></h2>
<span id="daydetail_render" runat="server" />
</div>
<div runat="server">
<asp:Calendar id="Calendar1" OnDayRender="Calendar1_Day
OnSelectionChanged="Calend
OnVisibleMonthChanged="Mon
DayStyle-Height="100" DayStyle-Width="75" DayStyle-HorizontalAlign="
DayStyle-verticalalign="To
NextPrevFormat="FullMonth"
TitleStyle-Font-Bold="Fals
TitleStyle-Font-Size="12" BackColor="white" BorderColor="#000000"
CellPadding="2" CellSpacing="2" Runat="server"
SelectedDayStyle-BackColor
SelectedDayStyle-ForeColor
OtherMonthDayStyle-ForeCol
DayStyle-BorderWidth="1" TodayDayStyle-ForeColor="B
Width="750">
</asp:Calendar><br><br>
</div>
then the code behind.....
Imports System.Data
Imports System.Data.SqlClient
Partial Class HR_Calendar_Default
Inherits System.Web.UI.Page
Public Structure MyDates
Dim _Title As String
Dim _Date As DateTime
End Structure
Public MyCollection As Collection
Public tempDate As DateTime
Public Sub Page_Load()
tempDate = Me.Calendar1.TodaysDate
If Not (Page.IsPostBack) Then
Get_DBItems()
End If
End Sub
Public Sub Get_DBItems()
Dim myCommand As SqlCommand
Dim ConnectionString As String = System.Configuration.Confi
Dim myConnection As System.Data.SqlClient.SqlC
myCommand = New SqlCommand("GetHRCalendarI
myCommand.CommandType = CommandType.StoredProcedur
'// Set to 40 days before the current value of tempDate
Dim parameterDate As New SqlParameter("@_Date", SqlDbType.DateTime)
parameterDate.Value = tempDate.AddDays(-40)
myCommand.Parameters.Add(p
'// Set to 40 days after the current value of tempDate
Dim parameterDate2 As New SqlParameter("@_Date_Plus_
parameterDate2.Value = tempDate.AddDays(40)
myCommand.Parameters.Add(p
Dim myDataReader As SqlDataReader
myConnection.Open()
myDataReader = myCommand.ExecuteReader()
Dim temp As MyDates
MyCollection = New Collection()
While (myDataReader.Read())
'//Created each time through the loop to get a new item to load and add to the collection
temp = New MyDates()
temp._Title = myDataReader.Item("Title")
temp._Date = CDate(myDataReader.Item("_
MyCollection.Add(temp)
End While
'// Close
myDataReader.Close()
myConnection.Close()
End Sub
Public Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
Dim Item As MyDates '//Temporary storage for looping through the collection
Dim DayHold As DateTime = "01/01/1900" '//Temporary date to check for multiple items per day
Dim MultipleItemDay As Boolean = False
'// Set to true if the date is in the collection and needs to be displayed
Dim DayTextHasChanged As Boolean = False
'//To build the string for the contents of the table cell
'//I'll use the stingbuilder class instead of concatenation
Dim temp As StringBuilder
If IsNothing(MyCollection) = True Then
Get_DBItems()
End If
For Each Item In MyCollection
'//The collection is loaded in date order. So, if the date is different than the DayHold variable
'//And I've already found the right day in the collection, I can exit the for loop
If DayHold <> Item._Date Then
If DayTextHasChanged = True Then
Exit For
End If
MultipleItemDay = False
DayHold = Item._Date
Else
MultipleItemDay = True
End If
'//If I've found a date matching the current date...I need to create the appropriate text
If e.Day.Date = Item._Date.ToString("d") Then
If MultipleItemDay = False Then
temp = New StringBuilder() '//Create a new stringbuilder object
Else
temp.Append("<br>") '//add a seperator to the stringbuilder
End If
temp.Append(Item._Title.To
temp.Append("</span>")
DayTextHasChanged = True '//Set the flag
End If
Next
'//If there was an Item for this day, add the stringbuilder text to the table cell
If DayTextHasChanged = True Then
e.Cell.Controls.Add(New LiteralControl(temp.ToStri
End If
End Sub
Public Sub Calendar1_SelectionChanged
Dim ConnectionString As String = System.Configuration.Confi
Dim myConnection As System.Data.SqlClient.SqlC
Dim myCommand As SqlCommand
Dim myDataReader As SqlDataReader
myCommand = New SqlCommand("GetHRCalendarS
myCommand.CommandType = CommandType.StoredProcedur
Dim parameter_Date As New SqlParameter("@_Date", SqlDbType.DateTime, 8)
parameter_Date.Value = Calendar1.SelectedDate
myCommand.Parameters.Add(p
myConnection.Open()
myDataReader = myCommand.ExecuteReader()
Dim temp As String = ""
Dim Category As String = ""
While (myDataReader.Read())
temp &= "Title : <a href=""edit.aspx?ID=" & myDataReader.Item("ID") & """>" & myDataReader.Item("Title")
End While
daydetail.Visible = True
daydetail_render.InnerHTML
myDataReader.Close()
myConnection.Close()
selectedday.InnerHTML = Calendar1.SelectedDate.ToS
tempDate = Calendar1.SelectedDate '//
Get_DBItems()
End Sub
Public Sub MonthChanged(ByVal source As Object, ByVal e As MonthChangedEventArgs)
'//Set the tempDatevariable to the value in the MonthChangedEventArgs NewDate property
tempDate = e.NewDate
'//Reload the collection
Get_DBItems()
End Sub
End Class
ASKER
True. Changed it problem remains.
When I launch it and it falls over I hover over tempDate = Me.Calendar1.TodaysDate
Me.Calendar1.TodaysDate is "11/15/2007" which is correct (although in the wrong format)
tempDate is #12:00am#
When I launch it and it falls over I hover over tempDate = Me.Calendar1.TodaysDate
Me.Calendar1.TodaysDate is "11/15/2007" which is correct (although in the wrong format)
tempDate is #12:00am#
The tempDate will never be Nothing because it's of type DateTime, which is NOT a class but structure.
The defaule value of tempDate will be DateTime.MinValue. If you try to decrease days backward from the min, the exception will be thrown say that it's out of range (between min and max dates).
The defaule value of tempDate will be DateTime.MinValue. If you try to decrease days backward from the min, the exception will be thrown say that it's out of range (between min and max dates).
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks but parameterDate.value was nothing
Shouldn't tempDate be set to '11/15/2007' on Page load and then used further down in DB_GetItems as the SQlparameter?
Shouldn't tempDate be set to '11/15/2007' on Page load and then used further down in DB_GetItems as the SQlparameter?
ASKER
"Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load"
Perfect! Many thanks.
One day I will need to try and get my head around all this public, private, protected stuff together with function signatures!
Perfect! Many thanks.
One day I will need to try and get my head around all this public, private, protected stuff together with function signatures!
What's showing on the page if this code was run
Response.Write(tempDate.To String("g" ))
Response.Write(tempDate.To
:) so fast
This is not valid signature of asp.net page load function.
It should be
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)