这是C#代码..
while (true)
{
DateTime dtNext = start.AddMinutes(duration);
if (start > end || dtNext > end)
break;
if (start < DateTime.Parse("12:00 PM"))
{
if (!doneMornHeader)
{
Label head = new Label();
head.Text = string.Format(header, "Morning");
this.Timediv.Controls.Add(head);
doneMornHeader = true;
}
morning = start.ToShortTimeString() + "-" + dtNext.ToShortTimeString();
Label lbl = new Label();
lbl.ID = "ImpLabel" + k;
lbl.Text = morning;
CheckBox cb = new CheckBox();
cb.ID = "ImpCheckbox" + k;
this.Timediv.Controls.Add(new LiteralControl("<div class='task low'>"));
this.Timediv.Controls.Add(new LiteralControl("<div class='desc'>"));
this.Timediv.Controls.Add(new LiteralControl("<div class='title'>"));
this.Timediv.Controls.Add(lbl);
this.Timediv.Controls.Add(new LiteralControl("<div class='make-switch span5 pull-right' data-on='success'>"));
this.Timediv.Controls.Add(cb);
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("<br>"));
k++;
}
else
{
if (!doneAfternoonHeader)
{
Label head = new Label();
head.Text = string.Format(header1, "Afternoon");
this.Timediv.Controls.Add(head);
doneAfternoonHeader = true;
}
afternon = start.ToShortTimeString() + "-" + dtNext.ToShortTimeString();
Label lbl1 = new Label();
lbl1.ID = "ImpLabel" + k;
lbl1.Text = afternon;
CheckBox cb1 = new CheckBox();
cb1.ID = "ImpCheckbox" + k;
this.Timediv.Controls.Add(new LiteralControl("<div class='task medium'>"));
this.Timediv.Controls.Add(new LiteralControl("<div class='desc'>"));
this.Timediv.Controls.Add(new LiteralControl("<div class='title'>"));
this.Timediv.Controls.Add(lbl1);
this.Timediv.Controls.Add(new LiteralControl("<div class='make-switch span5 pull-right' data-on='success'>"));
this.Timediv.Controls.Add(cb1);
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("</div>"));
this.Timediv.Controls.Add(new LiteralControl("<br>"));
k++;
}
start = dtNext;
}
现在我想找出while循环的最后一次迭代...
在上一次迭代中我想要改变
this.Timediv.Controls.Add(new LiteralControl("<div class='task low'>"));
到
this.Timediv.Controls.Add(new LiteralControl("<div class='task low last'>"));
if和else都是。
即。在上午和下午的会议中..
如何找到最后一次迭代?
答案 0 :(得分:1)
这是你的最后一次迭代:
if (start > end || dtNext > end)
break;
此外,在您进入最后一次迭代之前,您可以引入一些小检查:
DateTime tempDtNext = start.AddMinutes(duration);
var tempStart = tempDtNext;
var isGoingToEnd = (tempStart > end || tempDtNext > end);
this.Timediv.Controls.Add(new LiteralControl(string.Format("<div class='task low {0}'>", isGoingToEnd ? "last" : string.Empty));
答案 1 :(得分:1)
是否有理由无法在break
之前添加控件?这似乎是您退出循环的地方:
while(true)
{
if (start > end || dtNext > end)
{
//add control
this.Timediv.Controls.Add(new LiteralControl("<div class='task low last'>"));
break;
}
//do stuff..
}
另外,如果您有可以终止循环的条件,为什么还要费心while(true)
?
应该是:
while(start < end || dtNext < end)
{
//...
}