AjaxToolKitCalendarExtender Set Initial View Date - c#

I am using the AjaxToolKit Calendar Extender in my application so the user can select their date of birth. Most users that will be using the system will be over the age of 60 so i want this to appear as the default view when the textbox contains no text, i have set the calendar to open up in year mode, but i am unable to get to default to a date 60 years in the past without having anything in the associated textbox.
what i am trying to achieve is what is the picture but when the user open the calendar extender when the textbox contains no values.

#ccStars Try a combination of DefaultView, SelectedDate and PopupButtonID.
DefaultView: will take you to the year mode which you have already done.
SelectedDate: will set a date the calendar is initialized with,
PopupButtonID: the id of a control to show the calendar popup when clicked. If this value is not set, the calendar will pop up when the textbox receives focus. So have a small calendar icon next to your textbox and give its id. This will solve your problem of getting a date only when it is selected in the calendar.
Above are server-side properties. The client-side properties of your interest are:
selectedDate and visibleDate: the difference is that the selectedDate is the value represented by the textbox, but visibleDate is the date currently visible in calendar.
More information here: http://www.asp.net/ajaxlibrary/act_calendar_reference.ashx

You can try to use the Calendar's SelectedDate attribute, like this:
protected void Page_Load(object sender, EventArgs e)
{
calendarExtender.SelectedDate = DateTime.Today.AddYears(-60);
...
}

I know there's not a clear way of doing this, but what if we tried some jury-rigging and testing?
If you set the SelectedDate using Cobra's answer above, I'm assuming it will default to the right date range.
Now, if afterwards, on the client-side, you did some JavaScript to clear out the TextBox, I wonder if the CalendarExtender would still keep the old date ranges?
Using jQuery, here's a quick script to do it. Assumption is that the TextBox's ID is txtDateOfBirth and the ClientIDMode is set to Static.
$('#txtDateOfBirth').val('');

Related

To Display only current date selected in datetime picker

I have a datetime picker in windows form. When i am selecting a date it shows the last selected date and current date. But i want to shows only current date always. It should not show last selected date and current date(both) on the selection dropdown.
public FieldControl()
{
InitializeComponent();
dtpDate.Value = DateTime.Now;
}
Hello Kumar is right,
but what is sense of selectable date and time component where is only one option to select?
Isn't better to make datetimepicker disabled? At least it's less confusing for user and by default actual date is selected when datetimepicker is dispayed.
Jaroslav
Try this :
you can get just today's date as a DateTime using the Today property:
dtpDate.Value = DateTime.Today;
if you want to show only current date in your datepicker then use this code:
dtpDate.MinDate = dtpDate.MaxDate = DateTime.Now;
For more information
Refer this link
You can't do it on the picker. But you can fake it. Resize the dateTimePicker so it only shows the pick button. Then place it next to a textbox. In the onClose event you write the selected value to the textbox, and set the value of the dateTimePicker back to Today.

Make dates in datetimepicker grey?

Im writing a hotel booking system with VS2012 which uses ms sql and in the booking i have two datetimepicker controls. When i select a date in the first one the date automatically gets transfered to the second datetimepicker. I now want when the user opens up the second datetimepicker not being able to select a date that is before the first date from the first datetimepicker. I dont want to use if method displaying a message i just want the dates to be "greyed" out.
Is that possible and how?
*edit (screenshot of how i would like it to be viewed or similar http://i43.tinypic.com/2zpimvs.jpg )
*edit2: its not the control(dateTimePicker2) it self i want to grey out, its the dates inside it that is smaller then the date selected in dateTimePicker1. The screenshot provided is in the area of what im looking for.
You could try this:
DateTimePicker2.MinValue = Convert.toDateTime(DateTimePicker1.SelectedDate);
EDIT
Standard controls don't support the grey-out of a datetimepicker. If you want to do this, you need to subclass the datetimepicker or look at a premade control like a DateEdit from devexpress (i think they even have free controls).
Change the second datepicker MinDate to first DatePicker selected date .. like below ..
rdpDatePicker2.MinDate = rdpDatePicker1.SelectedDate.Value;

ASP.NET 4 Calendar control doesn't show SelectedDate

I have an ASP.NET 4 calendar display problem. I want to programmatically set the calendar. So, I put this code in the void Page_Load(object sender, EventArgs e) function:
Calendar1.SelectedDate = DateTime.Parse("2011-07-25");
it works fine. But, it displays the current month of the calendar (August) instead of the date I selected (July). I have to select back one month to see the date that I programmatically assigned.
What should i do?
Thanks!
You need to set the VisibleDate property to the month that you want it to show =)
There is a gotcha that you should be aware of.
If you set the selected date to DateTime.Now or something based on it such as DateTime.Now.AddYears(1) then the selected date will not be highlighted in the calendar even though it is selected.
You need to remove the time component using DateTime.Now.Date so that the calendar can do the comparison correctly when rendering. It took me a bit of debugging in the DayRenderEventHandler to figure that out.
I hope that somebody finds it useful.

How to set win forms datetime picker to have no default value?

I am implementing search functionality in WinForms and I search by date range. Thus there are dateForm and dateTo date pickers on the form. By default their values are date time now() and if user do not touch date time pickers at all he will not get any results. Because search will be performed between now() and now(), also if I put min and max values as default it would solve first problem but there would be another problem if user wants to search by date range, he would need to click many times to come from default 1700 (something) to now()
Any suggestions to solve this problem?
Thanks a lot.
You can't have a valueless datepicker with the out-of-the-box control. Why? It is backed by DateTime, which is non-nullable.
You can disable it with another control, or leave it disabled until the user clicks (bad UX for keyboard enthusiasts, like myself), or find or create (!) one that uses Nullable<DateTime>.
Edit:
In response to your comment, yes, you can do this; in fact, I've done it.
use fields or private properties to hold the 'from' and 'to' dates, instead of reading them from the dtp, and set their defaults to min and max
use a boolean flag to indicate when you are manipulating the dtp value in code, and in the dtp's ValueChanged event, set the flag's value to false
in the form load event, set the flag to true and dtp value to today's date
also in the ValueChanged event, set the from and to fields to the values of the dtps (you have to set both when either dtp changes, because the user will see the other one as set to today, but the search value will still be min or max).
The problems with this is that once the user has changed the date selection, she can't easily go back to "all dates." Furthermore, the user can't select "today only" without first changing one of the dates and then changing it back.
I think the best solution for you is to have a checkbox, "search by date range," which either enables the two dtps that are otherwise disabled, or displays the dtps that are otherwise hidden. Then you search from min to max unless the checkbox is checked, and when the checkbox is checked, you use the two dtp dates no matter what they are. Don't forget to deal with to and from being out of order, which can be done in several ways.
Have a look here for a nullable datetimepicker on CodeProject, in fact there are a few here.
Put a check box next to each datetime picker, and use the check box to enable/disable the datetime picker.
So if the datetimepicker is disabled, you know the user do not want to specify the datetime.
You can set DateDateTimePicker.Format property to Custom.
Then set DateDateTimePicker.CustomFormat property to your default text (e.g "N/A" or a space " ")
After the user has selected a certain date value, you should set back the format property to short etc.
Hope this helps!
The DateTimePicker has a ShowCheckBox property that "Determines whether a check box is displayed in the control. When the box is unchecked, no value is selected."
I've used something like the following for date range selectors that can be empty. One user told me that at first, she didn't know what the check box was for, but she figured it out on her own after using it a couple of times.
public DateTime? EndDate
{
get
{
DateTime? returnValue = null;
if (endDateDateTimePicker.Checked)
{
returnValue = endDateDateTimePicker.Value;
}
return returnValue;
}
set
{
if (value.HasValue)
{
endDateDateTimePicker.Checked = true;
endDateDateTimePicker.Value = value.Value;
}
else
{
endDateDateTimePicker.Checked = false;
}
}
}

datagridview datetimepicker C# help

I have a datagridview with 10 columns. Every month the user will update new info in the grid. I would like to have a datetimepicker next to the binding navigator. The purpose of the datetimepicker is to scroll forward or backward month by month. If scrolling forward the datagridview creates a new datagridview with the same 10 columns populated with certain data. Then the user can just add the necessary data for the new month. if the user scrolls backward a month, the datagridview shows past gridviews and columns the user has already updated in the past. This is sort of a hybrid of a datagridview and a monthly calendar sort of thing. I've looked everywhere and cannot find any ideas on how to add this datetimepicker control with functionality.
Any idea would be really helpful.
Use the OnValueChanged for the DateTimePicker to update the Filter on the BindingSource.
The DateTimePicker has an event that signifies when it's value has changed, you want to hook into that.
Once that event has fired you want to take the value of the DateTimePicker, and set the filter on the binding source to something like, "Date = '" + datePicker.toShortDateString() + "'"
This binding source should control what your datagridview displays.

Categories

Resources