在网上找了一下资料 发现并没有 this.FindControl这个命令,最后找到 this.Controls.Find 这个命令可用。
days = DateTime.DaysInMonth(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month);
//隐藏多出的控件
hideControls();
DateTime lastDay = Convert.ToDateTime(dateTimePicker1.Value.Year + "-" + dateTimePicker1.Value.Month + "-" + days.ToString());
for (int i = 1; i <= days; i++)
{
string weekString = Convert.ToDateTime(firstDay.AddDays(i-1)).DayOfWeek.ToString();
switch (weekString)
{
case "Monday": weekString = "星期一"; break;
case "Tuesday": weekString = "星期二"; break;
case "Wednesday": weekString = "星期三"; break;
case "Thursday": weekString = "星期四"; break;
case "Friday": weekString = "星期五"; break;
case "Saturday": weekString = "星期六"; break;
case "Sunday": weekString = "星期日"; break;
}
string labelName = "l" + i.ToString();
Control[] control = this.Controls.Find(labelName, true);
if (control.Length == 1 && (control[0] is Label))
{
(control[0] as Label).Text = i.ToString () + "(" + weekString + ")";
}
}