Prevent user from selecting past date in AjaxToolkit Calendar Extender Using ajaxtoolkit 3.0.20820

Posted by Sekar.C under ASP.NET AJAX on 4/2/2014 | Points: 10 | Views : 19638 | Status : [Member] | Replies : 3
Hi,

Prevent user from selecting past date in AjaxToolkit Calendar Extender
Using ajaxtoolkit 3.0.20820.0 version
Note:
Ajaxtoolkit latest version I got a solution
protected void Page_Load(object sender, EventArgs e)
{
Calendar Extender.StartDate=DateTime.Now;
}
but I need solution for ajaxtoolkit 3.0.20820.0 version

Regards
Sekar.c



Responses

Posted by: A2H on: 4/2/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
As per you description I understood that you want a solution for the particular version of Ajax calendar extender(3.0.20820.0).

You can try with the below code to show a alert message when user select past date. also the below code to will set the value to today's date. if you dont want to show alert you can comment that line.

Please use the below javascript to restrict users from selecting past date
Javascript Function
<script type="text/javascript">
function checkDate(sender, args) {

//Check if the date selected is less than todays date
if (sender._selectedDate < new Date()) {
//show the alert message
alert("You cannot select a day earlier than today!");
//set the selected date to todays date in calendar extender control
sender._selectedDate = new Date();

// set the date back to the current date
sender._textbox.set_Value(sender._selectedDate.format(sender._format))

}
}
</script>


Complete Code:
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CalendarExtender runat="server" OnClientDateSelectionChanged="checkDate" TargetControlID="TextBox1">
</asp:CalendarExtender>

Javascript function:
<script type="text/javascript">
function checkDate(sender, args) {

//Check if the date selected is less than todays date
if (sender._selectedDate < new Date()) {
//show the alert message
alert("You cannot select a day earlier than today!");
//set the selected date to todays date in calendar extender control
sender._selectedDate = new Date();

// set the date back to the current date
sender._textbox.set_Value(sender._selectedDate.format(sender._format))

}
}
</script>

You can check the below link for more details
http://stackoverflow.com/questions/5608062/how-to-disable-previous-dates-in-calendarextender-control-through-its-render-eve
Please Mark my reply as answer if it helps to resolve your issue

Thanks,
A2H
My Blog

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 4/2/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
Another suggestion is to create your calendar extender by customizing the ajax control.
You can check the below link for more details
http://www.codeproject.com/Articles/259776/Extending-an-AJAX-Toolkit-Control-Disabling-past-d

Thanks,
A2H
My Blog

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 4/2/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
The code which I have provided above will show an alert message when ever you select a previous date.But if you dont want to show an alert and you want all the dates to be disabled when ever you open the calendar extender you can try with the code suggested in this link
http://www.aspsnippets.com/Articles/AJAX-CalendarExtender-Disable-Past-Date-Selection.aspx

Thanks,
A2H
My Blog

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response