创建标签和; CheckBoxes一个接一个地动态

时间:2013-11-23 11:36:49

标签: c# asp.net

<asp:WizardStep ID="WizardStep3" runat="server" Title="Step 3">
<div class="content">
    <h1>Matchmaking starts at 
        <asp:DropDownList ID="StartTime_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2">
            <asp:ListItem Text="9:00 AM"></asp:ListItem>
            <asp:ListItem Text="9:30 AM"></asp:ListItem>
            <asp:ListItem Text="10:00 AM"></asp:ListItem>
            <asp:ListItem Text="10:30 AM"></asp:ListItem>
            <asp:ListItem Text="11:00 AM"></asp:ListItem>
            <asp:ListItem Text="11:30 AM"></asp:ListItem>
            <asp:ListItem Text="12:00 PM"></asp:ListItem>
            <asp:ListItem Text="12:30 PM"></asp:ListItem>
            <asp:ListItem Text="1:00 PM"></asp:ListItem>
            <asp:ListItem Text="1:30 PM"></asp:ListItem>
            <asp:ListItem Text="2:00 PM"></asp:ListItem>
            <asp:ListItem Text="2:30 PM"></asp:ListItem>
            <asp:ListItem Text="3:00 PM"></asp:ListItem>
            <asp:ListItem Text="3:30 PM"></asp:ListItem>
            <asp:ListItem Text="4:00 PM"></asp:ListItem>
            <asp:ListItem Text="4:30 PM"></asp:ListItem>
        </asp:DropDownList>
        and ends at 
          <asp:DropDownList ID="EndTime_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2">
              <asp:ListItem Text="9:00 AM"></asp:ListItem>
              <asp:ListItem Text="9:30 AM"></asp:ListItem>
              <asp:ListItem Text="10:00 AM"></asp:ListItem>
              <asp:ListItem Text="10:30 AM"></asp:ListItem>
              <asp:ListItem Text="11:00 AM"></asp:ListItem>
              <asp:ListItem Text="11:30 AM"></asp:ListItem>
              <asp:ListItem Text="12:00 PM"></asp:ListItem>
              <asp:ListItem Text="12:30 PM"></asp:ListItem>
              <asp:ListItem Text="1:00 PM"></asp:ListItem>
              <asp:ListItem Text="1:30 PM"></asp:ListItem>
              <asp:ListItem Text="2:00 PM"></asp:ListItem>
              <asp:ListItem Text="2:30 PM"></asp:ListItem>
              <asp:ListItem Text="3:00 PM"></asp:ListItem>
              <asp:ListItem Text="3:30 PM"></asp:ListItem>
              <asp:ListItem Text="4:00 PM"></asp:ListItem>
              <asp:ListItem Text="4:30 PM"></asp:ListItem>
              <asp:ListItem Text="5:00 PM"></asp:ListItem>
              <asp:ListItem Text="5:30 PM"></asp:ListItem>
              <asp:ListItem Text="6:00 PM"></asp:ListItem>

          </asp:DropDownList>. 
                                <br />
        <br />
        Each slot is
        <asp:DropDownList ID="SlotDuration_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2" OnSelectedIndexChanged="SlotDuration_DDL_SelectedIndexChanged">

            <asp:ListItem>10</asp:ListItem>
            <asp:ListItem>15</asp:ListItem>
            <asp:ListItem>20</asp:ListItem>
            <asp:ListItem>25</asp:ListItem>
            <asp:ListItem>30</asp:ListItem>
            <asp:ListItem>35</asp:ListItem>
            <asp:ListItem>40</asp:ListItem>
            <asp:ListItem>45</asp:ListItem>
            <asp:ListItem>50</asp:ListItem>
            <asp:ListItem>55</asp:ListItem>
            <asp:ListItem>60</asp:ListItem>
        </asp:DropDownList>
        minutes long. </h1>

</div>

我背后的代码是:

    DateTime start = DateTime.Parse(StartTime_DDL.SelectedItem.Text);
    DateTime end = DateTime.Parse(EndTime_DDL.SelectedItem.Text);
    double duration = double.Parse(SlotDuration_DDL.SelectedItem.Text);
    string morning = "";
    string afternon = "";
    while (true)
    {
        DateTime dtNext = start.AddMinutes(duration);
        if (start > end || dtNext > end)
            break;
        if (start < DateTime.Parse("12:00 PM"))
        {
            morning += start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";

            Label lbl = new Label();
            lbl.Text = morning;
            CheckBox cb = new CheckBox();

            this.Timediv.Controls.Add(lbl);

            this.Timediv.Controls.Add(cb);

        }
        else
        {
            afternon += start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";
            Label lbl1 = new Label();
            lbl1.Text = afternon;
            CheckBox cb1 = new CheckBox();

            this.Timediv.Controls.Add(lbl1);

            this.Timediv.Controls.Add(cb1);
        }
        start = dtNext;
    }
    if (morning.Length > 0)
        morning = "<div class='priority low'><span><strong>Morning</strong></span></div>" + morning;
    if (afternon.Length > 0)
        afternon = "<div class='priority medium'><span><strong>Afternoon</strong></span></div>" + afternon;

在下一级重复上一个标签的文字也显示.....

即,

9:00-9:10 CheckBox


9:00-9:10

9:10-9:20 CheckBox


9:00-9:10

9:10-9:20

9:20-9:30 CheckBox


...... Likethat .....

但我想要

9:00-9:10 CheckBox


9:10-9:20 CheckBox


9:20-9:30 CheckBox


怎么办...... ????

1 个答案:

答案 0 :(得分:2)

我认为您只想删除morningafternon变量上的某些连接。 +=将获取现有值并连接。 strA += strBstrA = strA + strB的简写。

试试这个:

DateTime start = DateTime.Parse(StartTime_DDL.SelectedItem.Text);
DateTime end = DateTime.Parse(EndTime_DDL.SelectedItem.Text);
double duration = double.Parse(SlotDuration_DDL.SelectedItem.Text);
string header = "<div class='priority low'><span><strong>{0}</strong></span></div>";
string morning = "";
string afternon = "";
bool doneMornHeader = false, doneAfternoonHeader = false;
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() + "<br>";
        Label lbl = new Label();
        lbl.Text = morning;
        CheckBox cb = new CheckBox();
        this.Timediv.Controls.Add(lbl);
        this.Timediv.Controls.Add(cb);
    }
    else
    {
        if(!doneAfternoonHeader)
        {
            Label head = new Label();
            head.Text = string.Format(header, "Afternoon");
            this.Timediv.Controls.Add(head);
                            doneAfternoonHeader = true;
        }
        afternon = start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";
        Label lbl1 = new Label();
        lbl1.Text = afternon;
        CheckBox cb1 = new CheckBox();
        this.Timediv.Controls.Add(lbl1);
        this.Timediv.Controls.Add(cb1);
    }
    start = dtNext;
}