Asp.Net Calendar: get date on page load

D

dean.h.brown

How do you get the Calendar date when the page first loads?

I have this code (which works once the user selects a date) - I get
"12:00:00 AM" for the label.text when the page first loads:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Label1.Text = Calendar1.SelectedDate
End Sub

Protected Sub Calendar1_SelectionChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Calendar1.SelectionChanged
Label1.Text = Calendar1.SelectedDate
End Sub

I actually have a ton of code that runs when the user changes the date
(pulling info from a database), but I'd like this code to run on the
first time the page loads.

I'd like to call the Calendar1_SelectionChanged sub in the page load
sub using:
"Call Calendar1_SelectionChanged(Calendar1,e)"
but I don't know how to pass the current date (unless I'm doing it
wrong!)

Thanks
 
M

Mark Rae

How do you get the Calendar date when the page first loads?
I have this code (which works once the user selects a date) - I get
"12:00:00 AM" for the label.text when the page first loads:

Yes, well you will because no date has been selected yet...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Calendar1.SelectedDate = DateTime.Now
Label1.Text = Calendar1.SelectedDate
End Sub
 
S

Scott M.

A calendar doesn't have a "current" date, as you put it. It has (possibly)
a selected date, and it has a month and year property. If you want the
current date the first time the page loads, just use the Date object.
 
D

dean.h.brown

Thanks to you both - I guess what I really want is for my code to run
when the page first loads.

Right now, the code doesn't run until the user picks the day (which
would usually be the current date, which is highlighted when the page
first loads)

I used the label.text as just an example - the real code grabs a bunch
of info from a database and displays it when ever the user changes the
day.

I could just copy it all from Calendar1_SelectionChanged to Page_Load,
but that seems inefficient (having it twice).
 
M

Mark Rae

I could just copy it all from Calendar1_SelectionChanged to Page_Load,
but that seems inefficient (having it twice).

So create a private function for it, and call it whenever you need to...
 
D

dean.h.brown

So create a private function for it, and call it whenever you need to...

Thanks - sometime the obvious, easy choice gets overlooked on those
late nights!
 

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,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top