I have some odd behaviour on my website. I have an ASP Calendar called UDExpiryCalendar that gets its values on page load. I read in a DateTime
from the User's record and populate the UDExpiryCalendar.VisibleDate
and UDExpiryCalendar.SelectedDate
then UDExpiryCalendar.DataBind();
I get these results:
- The date is in the past - The correct month is displayed, the correct year is displayed and the correct date is selected.
- The date is in this month - The correct month is displayed, the correct year is displayed, the current day is greyed and the correct date is selected (even if a few days in the future).
- The date is in the future - The correct month is displayed, the correct year is displayed but the selected date is NOT selected
Any ideas?
C#
var userID = Request.QueryString["ID"];
string getUserInfo = "select * from UserList where Id =" userID;
SqlCommand getUserInfocmd = new SqlCommand(getUserInfo, con);
getUserInfocmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader sqlDR = getUserInfocmd.ExecuteReader();
dt.Load(sqlDR);
sqlDR.Close();
DateTime oldExpiryDate = Convert.ToDateTime(dt.Rows[0][6]);
UDExpiryCalendar.VisibleDate = oldExpiryDate;
UDExpiryCalendar.SelectedDate = oldExpiryDate;
UDExpiryCalendar.DataBind();
ASP
<asp:Calendar ID="UDExpiryCalendar" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px">
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<NextPrevStyle VerticalAlign="Bottom" />
<OtherMonthDayStyle ForeColor="#808080" />
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<WeekendDayStyle BackColor="#FFFFCC" />
</asp:Calendar>
CodePudding user response:
I think the problem is that your date has time components within it. I tested this and it worked(in Page_Load):
Calendar1.SelectionMode = CalendarSelectionMode.Day;
Calendar1.TodaysDate = DateTime.Now.Date;
Calendar1.SelectedDates.Clear();
Calendar1.SelectedDate = DateTime.Now.AddYears(2).Date;
Calendar1.VisibleDate = DateTime.Now.AddYears(2).Date;