Thanks for the code. This is very useful. I added the capability to set the date to today's date (easily editable) when the control loads. I did this server side, as client side does not have an onload event.
dpStartDate and dpEndDate are date picker controls for providing a date range. The image directory was changed from your example to fit my example directory structure.
Protected Sub dpStartDate_ChildrenCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles dpStartDate.ChildrenCreated
SetDatePickerToCurrentDate(sender, e)
End Sub
Protected Sub dpEndDate_ChildrenCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles dpEndDate.ChildrenCreated
SetDatePickerToCurrentDate(sender, e)
End Sub
Protected Sub SetDatePickerToCurrentDate(ByVal sender As Object, ByVal e As System.EventArgs)
Dim picker As Telerik.WebControls.RadDatePicker = CType(sender, Telerik.WebControls.RadDatePicker)
picker.SelectedDate = Today.Date
'there is no way to accomplish the following that I have found
' picker.DateInput.Attributes("onload") = String.Format("var date = window.{0}.GetDate();date.setDate(date.getDate() + 1);window.{0}.SetDate(date);", picker.ClientID, picker.DateInput.ClientID, picker.MinDate.Year, picker.MinDate.Month - 1, picker.MinDate.Day)
picker.PopupButton.Attributes(
"onclick") = String.Format("if (window.{0}.GetDate().toString() == new Date({2}, {3}, {4}).toString()){{window.{0}.SetDate(new Date());}} return window['{0}'].TogglePopup();", picker.ClientID, picker.DateInput.ClientID, picker.MinDate.Year, picker.MinDate.Month - 1, picker.MinDate.Day)
picker.PopupButton.Attributes(
"onkeypress") = String.Format("if (window.{0}.GetDate().toString() == new Date({2}, {3}, {4}).toString()){{window.{0}.SetDate(new Date());}} return window['{0}'].TogglePopup();", picker.ClientID, picker.DateInput.ClientID, picker.MinDate.Year, picker.MinDate.Month - 1, picker.MinDate.Day)
picker.DateInput.Attributes(
"onfocus") = String.Format("if (window.{0}.GetDate().toString() == new Date({2}, {3}, {4}).toString()){{window.{0}.SetDate(new Date()); this.select();}} return false;", picker.ClientID, picker.DateInput.ClientID, picker.MinDate.Year, picker.MinDate.Month - 1, picker.MinDate.Day)
Dim clearButton As New ImageButton()
clearButton.Attributes(
"onclick") = String.Format("window.{0}.DateInput.Clear(); return false;", picker.ClientID)
clearButton.ImageUrl =
"~/Images/Delete.gif"
picker.Controls.Add(clearButton)
End Sub
Thanks again for the code sample,
Raymond