ASP.NET 4 Calendar control doesn't show SelectedDate - c#

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.

Related

CalendarView in UWP C# showing wrong day

I have CalendarView on two pages.
First page is used to add records to calendar by assigning the selected date to the database record.
Second page is used to show all records assigned to the selected date.
Now, when on the first page I assign date to the product by clicking the date manually, on the second page product is shown correctly when selected date that corresponds to the products date.
However, when on the first page I add dates to the products programmatically, on the second page, these products are shown one day before than they should.
I have no idea what is happening. I will post code if you want to check something.
THe problem is strange, becaue on one calendar these automatically added dates are shown for example on 5th April, but on the second calendar they are listed under 4th April.
Is that the globalization of two calendars problem?
Locale(globalization ) would be a potential problem. But having code would help us to provide you more info.
I have managed to resolve the problem. It was in deed issue with DateTimeOffset/DateTime values.
I changed every instance of Date I found in the code by appending it with
.LocalDateTime
.
This way I got the correct local date of each DateTimeOffset object and data is added to/ pulled from the Calendar correctly.

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;

AjaxToolKitCalendarExtender Set Initial View Date

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('');

how to set raddatepicker date input box with c#

I am trying to set the date to "todays " date in the rad date picker but all it does is it selects it in the calendar but the box where the date is displayed is empty (i.e 12/2/2012) is not showing... Here is my line of code, (i have seen some examples in google but they are all javascript, i need to do it on the back end C#).
rdpShipDate.Calendar.SelectedDate = DateTime.Today;
That line above just selecs the date in the calendar but the box is still empty.
Any help would be much appreciated.
Thanks
Have you tried:
rdpShipDate.SelectedDate = DateTime.Today;

Telerik RadCalendar ASP.NET control reset date

I use Telerik RadCalendar ASP.NET control. How I can in code reset selected date? (Set no selected Date)
From their site:
http://demos.telerik.com/aspnet-ajax/calendar/examples/programming/clientsideapi/defaultcs.aspx
unselectDate(date); //Takes a triplet representation of a date and if valid deselects it in the calendar.
unselectDates(dates); //Takes an array of triplets representing dates and if valid deselects them in the calendar.
First you can find what dates are selected with get_selectedDates()
Then put them in the unselect method.
var selectedDates = get_selectedDates();
unselectDates(selectedDates);
If you dont know what dates are selected maybe you can reload the calendar.
If you want to clear the selection server-side then this should work:
RadCalendarObject.SelectedDates.Clear();

Categories

Resources