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";
}
}
Related
The following control was in the code.
Below .aspx will the be the start of the .aspx.cs that starts out Page_Load.
It is allowing the date to be changed in the variable, but the interface doesn't show an error.
I am using the following tutorial.
http://learningdes.peterblum.com/Validation/Validators/Controls/RangeValidator.aspx
<des:MonthYearTextBox ID="dtbEndMonth" runat="server" Font-Size="8" AutoHint="false">
</des:MonthYearTextBox>
<des:DateTextBox ID="dtbEndDate" runat="server" AutoHint="false" CssClass="DateTextBox"
PopupCalendar-ToggleImageAlign="bottom" PopupCalendar-
ToggleImageUrl="~/Images/calendar_view_month.png">
</des:DateTextBox>
<des:DataTypeCheckValidator ID="DataTypeCheckValidator2"
ControlIDToEvaluate="dtbEndDate"
runat="server" DataType="Date"
ErrorMessage="The correct format for this date is MM/dd/yyyy"
Group="DateRange">
<ErrorFormatterContainer>
<des:TooltipImageErrorFormatter ImageURL="~/Images/error_small.png"
Display="Dynamic" />
</ErrorFormatterContainer>
</des:DataTypeCheckValidator>
<des:RangeValidator ID="EndDateRangeValidator" runat="server"
ControlIDToEvaluate="dtbEndDate"
ErrorMessage="End Date must be between 2005 and {Maximum}"
Minimum="01/01/2005"
Group="DateRange">
<ErrorFormatterContainer>
<des:TooltipImageErrorFormatter ImageURL="~/Images/error_small.png"
Display="Dynamic" />
</ErrorFormatterContainer>
<HiliteFields>
<des:ControlConnection ControlID="dtbEndDate" />
</HiliteFields>
</des:RangeValidator>
protected void Page_Load(object sender, EventArgs e)
{
StartDateRangeValidator.MaximumAsNative = DateTime.Today;
EndDateRangeValidator.MaximumAsNative = DateTime.Today;
...
Hi i am busy creating a asp.net project that needs to get three values from the user. I am using a textbox with a range textmode to get these values and the first time the user moves the slider it works perfectly but after that the user can.t adjust any of the textboxes anymore.
I tried using a slider extender but this does not seem to work and only a textbox shows.
my html code to create the textboxes and labels showing the value
body>
<form id="form1" runat="server">
<p>
Quantum 1 Length:
<asp:TextBox ID="txtVal1" runat="server" TextMode="Range" min="1" Max="8" OnTextChanged="txtVal1_TextChanged1" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblVal1" runat="server" Text="/////"></asp:Label>
</p>
<p>
Quantum 2 Length:
<asp:TextBox ID="txtVal2" runat="server" TextMode="Range" min="1" Max="8" OnTextChanged="txtVal2_TextChanged1" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblVal2" runat="server" Text="/////"></asp:Label>
</p>
<p>
Quantum 3 Length:
<asp:TextBox ID="txtVal3" runat="server" TextMode="Range" min="1" Max="8" AutoPostBack="True" OnTextChanged="txtVal3_TextChanged1"></asp:TextBox>
<asp:Label ID="lblVal3" runat="server" Text="/////"></asp:Label>
</p>
<p>
<asp:Button ID="btnNext" runat="server" AutoPostBack="true" Height="24px" OnClick="btnNext_Click" style="margin-left: 0px" Text="Next" Width="150px" />
<script type="text/javascript" __designer:mapid="129">
function NextPage() {
window.open('Execution.aspx', 'Execution');
}
</script>
<asp:Label ID="lblError" runat="server" ForeColor="Red" Text="Invalid input; Please ensure that Quantum 1 <= Quantum 2 <= Quantum 3" Visible="False"></asp:Label>
</p>
</form>
</body>
My C# code used to control the boxes and change their values
public partial class Quantums : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
txtVal1.Text = HttpContext.Current.Session["Q1"].ToString();
}
catch { }
try
{
txtVal2.Text = HttpContext.Current.Session["Q2"].ToString();
}
catch { }
try
{
txtVal3.Text = HttpContext.Current.Session["Q3"].ToString();
}
catch { }
lblVal1.Text = txtVal1.Text;
lblVal2.Text = txtVal2.Text;
lblVal3.Text = txtVal3.Text;
//UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger() { ControlID = "btnNext" });
}
protected void txtVal1_TextChanged1(object sender, EventArgs e)
{
HttpContext.Current.Session["Q1"] = txtVal1.Text;
}
And then the same code for textbox 2 and 3. Any suggestions on why the textboxes vaklue keeps resetting and autopostback is enabled on all three buttons.
I just noticed you were missing if(Page.IsPostBack()) return;. Your problem is that every time you change your textbox, the 'Page_Load' event is enacted, then the respective event handler (see page life cycle). So you were setting the text to the sessions, then setting the sessions to the text, effectively doing nothing.
You actually don't need anything for this page in the Page_load method, at least for now. Try the following:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtVal1_TextChanged(object sender, EventArgs e)
{
Session["Q1"] = txtVal1.Text;
lblVal1.Text = txtVal1.Text;
}
protected void txtVal2_TextChanged(object sender, EventArgs e)
{
Session["Q2"] = txtVal2.Text;
lblVal2.Text = txtVal2.Text;
}
protected void txtVal3_TextChanged(object sender, EventArgs e)
{
Session["Q3"] = txtVal3.Text;
lblVal3.Text = txtVal2.Text;
}
You can then access the user input values using the respective session values. For example:
protected void Submit(object sender, EventArgs e)
{
List<string> answers = new List<string>();
answers.Add(Session["Q1"].ToString());
answers.Add(Session["Q2"].ToString());
answers.Add(Session["Q3"].ToString());
// Do stuff with answers
}
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.
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();
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;
}
}