Answered by:
calendar question

Question
-
User1298215938 posted
Hi I want my calendar to auto pick today's date but I also want the user to be able to select another date this is my current code.
Calendar1.SelectedDate = DateTime.Now;
Wednesday, August 19, 2020 6:49 PM
Answers
-
User-1330468790 posted
Hi MOHIIMRAN,
I think you are using the server control "Calendar" which allows you to select any date.
The problem is that what purpose you want to achieve.
For example, if I want to implement a calendar that automatically selects today, I simply assign today to the calendar when it is rendered for the first time. Then, I could use the event "OnSelectionChanged" to do another stuff when users select another date.
More details, you could refer to below codes:
aspx:
<form id="form1" runat="server"> <div> <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" ></asp:Calendar> </div> <div> <asp:Label ID="displayCalendar" runat="server" ></asp:Label> </div> </form>
code behind:
protected void Page_Load(object sender, EventArgs e) { // Render today for the first time if (!IsPostBack) { Calendar1.SelectedDate = DateTime.Now; displayCalendar.Text = Calendar1.SelectedDate.ToString(); } } // Change the label whenever the calendar's selected date has been changed. protected void Calendar1_SelectionChanged(object sender, EventArgs e) { displayCalendar.Text = Calendar1.SelectedDate.ToString(); }
Demo:
Feel free to let me if you still questions on this topic.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 20, 2020 2:06 AM
All replies
-
User409696431 posted
It would help if you explained which Calendar you are using.
Wednesday, August 19, 2020 8:37 PM -
User-1330468790 posted
Hi MOHIIMRAN,
I think you are using the server control "Calendar" which allows you to select any date.
The problem is that what purpose you want to achieve.
For example, if I want to implement a calendar that automatically selects today, I simply assign today to the calendar when it is rendered for the first time. Then, I could use the event "OnSelectionChanged" to do another stuff when users select another date.
More details, you could refer to below codes:
aspx:
<form id="form1" runat="server"> <div> <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" ></asp:Calendar> </div> <div> <asp:Label ID="displayCalendar" runat="server" ></asp:Label> </div> </form>
code behind:
protected void Page_Load(object sender, EventArgs e) { // Render today for the first time if (!IsPostBack) { Calendar1.SelectedDate = DateTime.Now; displayCalendar.Text = Calendar1.SelectedDate.ToString(); } } // Change the label whenever the calendar's selected date has been changed. protected void Calendar1_SelectionChanged(object sender, EventArgs e) { displayCalendar.Text = Calendar1.SelectedDate.ToString(); }
Demo:
Feel free to let me if you still questions on this topic.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 20, 2020 2:06 AM