How to disable the future dates in ajax calender extender - c#

This is the code i am using in my aspx page
<asp:TextBox ID="ReqFromTxtBox" runat="server" class="textBox" Width="155px" onKeyPress="javascript: return false;" onPaste="javascript: return false;">
</asp:TextBox>
<cc1:CalendarExtender ID="ReqFromTxtBox_CalendarExtender" runat="server" PopupButtonID="ReqFromCalendarBtn" TargetControlID="ReqFromTxtBox">
</cc1:CalendarExtender>

For diabling Future Dates on pageload event set EndDate to Today Date using DateTime.Now:
protected void Page_Load(object sender, EventArgs e)
{
ReqFromTxtBox_CalendarExtender.EndDate = DateTime.Now; //to disable future Dates
}
and for Disabling past dates you have to set StartDate to Today :
protected void Page_Load(object sender, EventArgs e)
{
ReqFromTxtBox_CalendarExtender.StartDate= DateTime.Now; //to disable past Dates
}
Reference Link How To Disable Past of Future Date of Calendar Extender

Related

Compare two dates in ASP calender

I am a noob at ASP, I am trying to take take a selected date from an ASP calendar and save that in a textbox or something that I can compare with the second selected date so that the second is larger. I'm not sure if it is possible while just using one calendar. I tried but do not know how to save the first date collection for comparison. I tried both ways but failed miserable. I did do a search but they are using javascript, or java and other languages I do not know.
What I want to do:
I am trying to take two separate inputted dates from user. when 1st date is inputted, store in something, then validate that the user selects a date after the 1st selected date. If not return error message
<asp:TextBox ID="response" runat="server" />
<asp:TextBox ID="caldate1" runat="server" />
<asp:TextBox ID="caldate2" runat="server" />
<asp:CompareValidator ID="calvalidae" runat="server" ControlToCompare="caldate1" ErrorMessage="Date should be later than first date" Type="Date" operator="GreaterThan" ValueToCompare="caldate2"></asp:CompareValidator>
<asp:Calendar ID="cal1" runat="server"></asp:Calendar>
<asp:Calendar ID="cal2" runat="server" SelectionMode="Day" OnSelectionChanged="cal1_SelectionChanged" ></asp:Calendar>
//serverside
protected void cal1_SelectionChanged(object sender, EventArgs e)
{
caldate1.Text = cal1.SelectedDate.ToShortDateString();
if (cal1.SelectedDate.Date > cal2.SelectedDate.Date)
{
caldate1.Text = "You selected ";
caldate1.Text += cal1.SelectedDate.ToShortDateString();
}
else
{
caldate1.Text = "Select a valid date";
}
}
If you just want to compare the dates in code behind do the below
Markup
<asp:CompareValidator ID="calvalidae" runat="server"
ControlToValidate ="caldate1" ValueToCompare="text" ControlToCompare="caldate2"
ErrorMessage="Date should be later than first date" Type="Date"
operator="GreaterThan" ></asp:CompareValidator><br/>
<asp:Calendar ID="cal1" runat="server" OnSelectionChanged="cal1_SelectionChanged1"></asp:Calendar><br/>
<asp:Calendar ID="cal2" runat="server" SelectionMode="Day" OnSelectionChanged="cal2_SelectionChanged" ></asp:Calendar><br/>
</div>
Code Behind
protected void cal1_SelectionChanged1(object sender, EventArgs e)
{
caldate1.Text = cal1.SelectedDate.ToShortDateString();
IsValidDate();
}
protected void cal2_SelectionChanged(object sender, EventArgs e)
{
caldate2.Text = cal2.SelectedDate.ToShortDateString();
IsValidDate();
}
private void IsValidDate()
{
response.Text = string.Empty;
if (cal1.SelectedDate > cal2.SelectedDate)
{
response.Text = "Date should be later than first date";
}
}

asp calendar : blocking off when navigating previous/ next month

i'm using asp calendar in my webpage, when i was clicking the previous month arrow its just showing that month and popping off. but if its month navigation it should display as per my requirement ? and its selection date it should pop off.
Below is my code :
<asp:Image ID="Image1" runat="server" Height="20px" ImageUrl="~/Images/calendar.GIF" Width="20px" onclick="displayCalendar()" />
<div id="datePicker"><asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" ></asp:Calendar></div>
function displayCalendar() {
var datePicker = document.getElementById('datePicker');
datePicker.style.display = 'block';
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
StartDate.Text = Calendar1.SelectedDate.ToString("d");
}
anyone's help will be appreciated .
thanks
Got my solution for this question.
OnsivibleMonthChanged="<function()>"
calendar control.
and defining the function to display the calendar.

How can I see my choice of day in calender from the toolbox?

I have a Textbox and a Calender:
<asp:TextBox ID="datepicker" runat="server" CssClass="input-date date-icn small-label" AutoCompleteType="BusinessZipCode" /asp:TextBox>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"
/asp:Calendar>
when i pick the date from my calender i want it to show on the textbox, how can i do it?
this is my code behind:
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Calendar Calendar = new Calendar();
Panel PanelCalendar = new Panel();
PanelCalendar.Controls.Add(Calendar);
}
please help me, thank you!
Just use Calendar.SelectedDate property instead?
Gets or sets the selected date.
Like;
datepicker.Text = Calendar1.SelectedDate.ToString();

'CompareEndTodayValidator' cannot be converted to type 'Date'.?

I have a calender control ,when selecting a date it should be displayed on an associated text box in "dd-mm-yyyy" format . And have a compare validator which should validate the selected date ,if it is greater than today's date. I have written like this
<asp:TextBox runat="server" ID="tb_date" BackColor="White" ></asp:TextBox>
<asp:Calendar ID="EndDate" runat="server"
OnSelectionChanged="EndDate_OnSelectionChanged"
</asp:Calendar>
<asp:CompareValidator runat="server" ID="CompareEndTodayValidator" ErrorMessage="Exam date can't be less than today"
ControlToValidate="tb_date" Type="Date" Operator="LessThanEqual" > </asp:CompareValidator>
protected void Page_Load(object sender, EventArgs e)
{
CompareEndTodayValidator.ValueToCompare = DateTime.Now.ToString("dd-MM-yyyy");
}
protected void EndDate_OnSelectionChanged(object sender, EventArgs e) //COMPARE VALIDATOR FOR EXAM DATE
{
tb_date.Text = EndDate.SelectedDate.ToString("dd-MM-yyyy");
}
It Shows an error
The value '26-09-2013' of the ValueToCompare property of
'CompareEndTodayValidator' cannot be converted to type 'Date'.
Please help. I have tried it with by changing type="string". but failed.When putting mm-dd-yyyy frmat it works properly .But I need in dd-mm-yyyy format
The problem is that the date format that you are converting your selected calendar value to is NOT compatible with the default DateTime.Parse, which is what the Comparer validator no doubt uses internally. Use a different date format or else use the CustomValidator control so you can control the date parse format manually.
DateTime date = DateTime.Parse("26-09-2013"); // Fails
I hope this helps.
EDIT - Using Custom Validator
<asp:CustomValidator runat="server" ID="CompareEndTodayValidatorCust" OnServerValidate="ServerValidation" ControlToValidate="tb_date" ValidateEmptyText="True" ErrorMessage="Exam date can't be less than today" />
protected void ServerValidation (object source, ServerValidateEventArgs arguments)
{
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string format = "dd-MM-yyyy";
DateTime dtToValidate = DateTime.ParseExact(tb_date.Text, format, provider);
arguments.IsValid = (dtToValidate <= DateTime.Now.AddDays(-1));
}
P.S.
Also in the form submit handler or page load method you'll want to check that Page.IsValid == true before allowing the save operation to proceed.
P.S.S
If you want to get more fancy you could provide a JavaScript method in the ClientValidationFunction property and validate client side too. That may be overkill though.
Try with CustomValidator like following
ASPX
<asp:TextBox runat="server" ID="tb_date" BackColor="White" ></asp:TextBox>
<asp:Calendar ID="EndDate" runat="server"
OnSelectionChanged="EndDate_OnSelectionChanged" ></asp:Calendar>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Invalid date">
</asp:CustomValidator><br />
Code behind:
protected void EndDate_OnSelectionChanged(object sender, EventArgs e) //COMPARE VALIDATOR FOR EXAM DATE
{
CustomValidator1.IsValid = true;
DateTime SelectedDate = EndDate.SelectedDate.Date;
DateTime NowDate = DateTime.Now;
tb_date.Text = SelectedDate.ToShortDateString();
if (SelectedDate.Date > NowDate.Date)
{
CustomValidator1.IsValid = false;
}
}

How do I handle this error : No overload for 'SQLDisplay_Date_records' matches delegate 'System.EventHandler'

So I basically have two textboxes in the aspx file which get populated by dates (using the jquery datepicker) as given below
Start Date: <asp:TextBox ID="TxtDatepicker_start" runat="server" Width = 125px >
</asp:TextBox>
End Date: <asp:TextBox ID="TxtDatepicker_end" runat="server" Width = 125px >
</asp:TextBox>
<asp:Button ID="Button_daterecords" runat="server" Text="Show records" OnClick ="SQLDisplay_Date_records" /><br />
However when I try to determine what has been stored in the text boxes for further processing in the code behind using this function
protected void SQLDisplay_Date_records()
{
string date1 = TxtDatepicker_end.Text;
string date2 = TxtDatepicker_end.Text;
}
I am getting this error
No overload for 'SQLDisplay_Date_records' matches delegate 'System.EventHandler'
Can someone explain this to me ,I apologize for the naive question but I am still making my way around with ASP.NET and C#
Wild guess, OnClick is a event. which has a object (source) and eventargs.
Try setting it to
Protected void SQLDisplay_Date_records(object sender, EventArgs e)
{
string date 1 = TxtDatepicker_end.Text;
string date 2 = TxtDatepicker_end.Text;
}
Your method signature is incorrect.
The protected void SQLDisplay_Date_records()
should be protected void SQLDisplay_Date_records(object sender, EventArgs e)
As it is an OnClick Event and it should take an Event Argument as a parameter.

Categories