通过最后选择的第一个取消选择在asp.net中选择/取消选择按钮

时间:2016-05-24 11:36:27

标签: c# asp.net

我已经动态创建按钮,如代码所示,所有按钮从10:00到12:00,间隔15分钟,并在lifo顺序中取消选择,如果我选择了10:0和10:15那么我就不能选择10:45 ..和ii选择了10:0,10:15,10:30,10:45然后我必须取消选择10:15然后首先我们必须取消选择10:30和10:45 ......请帮帮我。

   private void GetControls()
    {
        count++;
        for (int i = 10; i < 12; i++)
        {
            for (int j = 0; j < 60; j += 15)
            {
                Button btn = new Button();
                btn.Text = i + "-" + j;
                btn.ID = i + "-" + j;
                btn.Command += new CommandEventHandler(this.btn_Click);
               // btn.Click += btn_Click;
                flag = true;
                btn.CommandName = i + "-" + j;
                if (count==1)
                {
                    PlaceHolder1.Controls.Add(btn);
                }
             }
       }   

    }

    private void btn_Click(object sender, CommandEventArgs e)
    {
        count++;

        string ID = (sender as Button).ID;
        Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
        Label1.Visible = false;
        Button btn = sender as Button;


        if (btn.BackColor == Color.Green)
        {
            btn.BackColor = System.Drawing.Color.Yellow;
            getStatus(sender);

        }

        else
        {
            btn.BackColor = System.Drawing.Color.Green;

        }

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我!您希望一次只选择一个按钮,这样如果单击一个按钮,其余按钮应该重置?如果是这种情况,这里有一个代码:

[编辑]

添加了一个会话以记住动态按钮ID并使用(。)代替( - )按钮ID

private void GetControls()
    {
        count++;
        for (int i = 10; i < 12; i++)
        {
            for (int j = 0; j < 60; j += 15)
            {
                Button btn = new Button();
                btn.Text = i + "-" + j;
                btn.ID = i + "." + j;
                btn.Command += new CommandEventHandler(this.btn_Click);
                // btn.Click += btn_Click;
                flag = true;
                btn.CommandName = i + "-" + j;
                if (count == 1)
                {
                    PlaceHolder1.Controls.Add(btn);
                    List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
                    if (!createdControls.Contains(btn.ID)) createdControls.Add(btn.ID);
                    Session["Controls"] = createdControls;
                }
            }
        }

    }

[已编辑]使用以下内容替换btn-Click事件。

private void btn_Click(object sender, CommandEventArgs e)
    {
        count++;
        string ID = (sender as Button).ID;
        ResetButton(Convert.ToDouble(ID));
        Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
        Button btn = sender as Button;

        if (btn.BackColor == Color.Green)
        {
            btn.BackColor = System.Drawing.Color.Yellow;


        }

        else
        {
            btn.BackColor = System.Drawing.Color.Green;

        }
    }

[编辑]这是方法实现

private void ResetButton(double selectedButtonID)
        {
            List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
            TimeSpan timespan = TimeSpan.FromHours(selectedButtonID);
            string currentSelectedTime = timespan.ToString("h\\:mm");
            foreach (string buttonID in createdControls)
            {
                if (!string.IsNullOrEmpty(buttonID))
                {
                    int comparisonResult = timespan.CompareTo(TimeSpan.FromHours(Convert.ToDouble(buttonID)));
                    Button button = Page.FindControl(buttonID) as Button;
                    if (button != null && comparisonResult==1)
                    {
                        button.BackColor = Color.Yellow;// selected
                    }
                    else
                    {
                        button.BackColor = Color.Red;// deselected
                    }
                }
            }