Original article exit http://www.dotblogs.com.tw/mis2000lab/archive/2013/06/10/calendar_visibledate_birthday_dropdownlist.aspx
[Exercise] Enter your birthday (year/month/day) #2 -- the time of the Calendar control jumps back to five years or ten years ago? -- TodaysDate and VisibleDate
Previous Article:[Exercise]Enter your birthday (Year/Month/Day)--Calendar(Calendar)Control time jump, one jump back five years, ten years ago?-- TodaysDateProperties,VisibleDateAttribute
Http://www.dotblogs.com.tw/mis2000lab/archive/2011/05/30/calendar_todaysdate.aspx
This exercise can be considered「 Previous set 」Ch.3Calendar Control.
Add the following features:
"Born"YearIf you want to include the one hundred-year option
We can't add sub-options by ourselves. It must be completed by writing programs.
How do I control the year?
SeeSystem. DateTimeThere are many available methods.
For example. AddYear ()Method, You can use-1 to subtract one year. Please referHttp://msdn.microsoft.com/zh-tw/library/system.datetime.addyears.aspx
For (int I = 0; I <101; I ++)
{
// = Method 1 =:
// Int myYear =System. DateTime. Now. Year -I;
// DropDownList1.Items. Add (myYear. ToString ());
// = Method 2 =:
DropDownList1.Items. Add (System. DateTime. Today. AddYears(-I). ToString ("Yyyy"));
}
Suggestion: Set System. DateTimeMoveForLoop outside,
Avoid calling the Now () or. AddYear () method for each running cycle to save resources.
Another key point is to "Create a calendar control ".RenderingThat year, that month 」
Use the calendar control's「VisibleDate"AttributeIt is better than the "TodaysDate" attribute.
As mentioned in MSDN:
IfVisibleDateAttributeNot set. The specified date TodaysDate attribute determines which month will be displayed in the Calendar control.
// = Method 1 =:
// Calendar1. TodaysDate=New DateTime(Convert. ToInt32 (DropDownList1.SelectedValue), Convert. ToInt32 (DropDownList2.SelectedValue), 1 );
// = Date in DateTime (Year/Month/Day). Only integers are allowed 」.
// = Method 2 =:
// *** It is recommended that you use the. VisibleDate attribute instead.
// *** Only the current month is displayedMonth」!
Calendar1. VisibleDate=NewDateTime(Convert. ToInt32 (DropDownList1.SelectedValue), Convert. ToInt32 (DropDownList2.SelectedValue), 1 );
// = Date in DateTime (Year/Month/Day). Only integers are allowed 」.
When the. VisibleDate attribute is used, when the month date is not "preset", the screen looks better.
This is a supplementary example of class,
Many "beginners 」ThoughtDo not write programs by yourself
Later, I found that they do not use string functions or date-and time-related functions.
It may not be the "Basic if/for/while Syntax" That won't be used. After all, I have learned a little bit in school before.