Possible Bug In the Calendar Control on web forms.

G

Guest

The calendar control only has the event of SelectionChanged to detect if the
user clicks on the calendar. I have 2 calendars on a form, one for arrival
date one for departure date. Every time a use clicks on the calendars, the
proper date gets filled in the text boxes below the calendars.

I noticed that if you click on the date correspinding to the current date,
the textboxes do not get filled or changed. Is this a bug? If not how can I
detect that the user clicked on today's date?
 
O

Otis Mukinfus

The calendar control only has the event of SelectionChanged to detect if the
user clicks on the calendar. I have 2 calendars on a form, one for arrival
date one for departure date. Every time a use clicks on the calendars, the
proper date gets filled in the text boxes below the calendars.

I noticed that if you click on the date correspinding to the current date,
the textboxes do not get filled or changed. Is this a bug? If not how can I
detect that the user clicked on today's date?

Actually, when processing data from controls on a form you should read
the data from the controls before processing a submit, so you can
validate the entries. What if the user manually changes one of the
text boxes without using the calendar control?

But, below is an example of how to accomplish your requirement.

You will need to accept that the default date is DateTime.Today. If
you design this way you will know that if the user does not choose a
date, the date is the default.

If the user chooses a new date the event will fire and you can handle
that. If the user has chosen a different date than today and then
chooses today as the date again, the event will fire, because today
becomes the SelectedDate.

1. Set Calendar.SelectedDate to DateTime.Today
2. Set TextBox.Text to DateTime.Today
3. If you want to capture the date of the Calendar/TextBox as an
event, set a variable to DateTime.Today

Now you have set the default date on load to today.

Supposing you have a private DateTime field named _userChosenDate, the
code below does as you want.

private DateTime _userChosenDate;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Calendar1.SelectedDate = DateTime.Today;
TextBox1.Text = DateTime.Today.ToString("MM/dd/yyyy");
_userChosenDate = DateTime.Today;

}


protected void Calendar1_SelectionChanged(object sender, EventArgs
e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString("MM/dd/yyyy");
_userChosenDate = Calendar1.SelectedDate;
}

I hope this has been helpful to you.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
O

Otis Mukinfus

On Sat, 28 Jan 2006 10:29:36 -0600, Otis Mukinfus

In line...

You must have already set the default date in the calendars to the
current date.

When investigating further I discovered if you do not set a date as
default in the calendar control it will fire the SelectionChanged
event even when you click the current date. To make this work as you
originally expected, do not set a default date on any of the calendar
controls.
Actually, when processing data from controls on a form you should read
the data from the controls before processing a submit, so you can
validate the entries. What if the user manually changes one of the
text boxes without using the calendar control?

But, below is an example of how to accomplish your requirement.

You will need to accept that the default date is DateTime.Today. If
you design this way you will know that if the user does not choose a
date, the date is the default.

If the user chooses a new date the event will fire and you can handle
that. If the user has chosen a different date than today and then
chooses today as the date again, the event will fire, because today
becomes the SelectedDate.

1. Set Calendar.SelectedDate to DateTime.Today
2. Set TextBox.Text to DateTime.Today
3. If you want to capture the date of the Calendar/TextBox as an
event, set a variable to DateTime.Today

Now you have set the default date on load to today.

Supposing you have a private DateTime field named _userChosenDate, the
code below does as you want.

private DateTime _userChosenDate;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Calendar1.SelectedDate = DateTime.Today;
TextBox1.Text = DateTime.Today.ToString("MM/dd/yyyy");
_userChosenDate = DateTime.Today;

}


protected void Calendar1_SelectionChanged(object sender, EventArgs
e)
{
TextBox1.Text = Calendar1.SelectedDate.ToString("MM/dd/yyyy");
_userChosenDate = Calendar1.SelectedDate;
}

I hope this has been helpful to you.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top