【问题标题】:Dynamically created buttons won't fire until second click直到第二次点击才会触发动态创建的按钮
【发布时间】:2013-04-02 22:36:30
【问题描述】:

我正在动态创建一个带有许多按钮的表格。目标是每次单击按钮时,它旁边的按钮都会改变颜色。但是,每次我点击一个按钮时,我点击的那个按钮都会改变它的颜色。只有在第二次点击后,它旁边的才会改变颜色。

我对此很陌生,而且我的英语不太好,所以如果你能把你的解释尽可能简单地说明一下,那将是最好的。

代码如下:

protected void Page_Load(object sender, EventArgs e)
{
    Table tavla = new Table(); //New Table
    tavla.GridLines = GridLines.Both; //Gridlines
    tavla.BorderWidth = 4; //BorderWidth
    tavla.ID = "tbl1"; //Table ID
    Button btn = null; //New Button
    for (int i = 1; i <= 8; i++)
    {
        TableRow myline = new TableRow();
        for (int j = 1; j <= 8; j++)
        {

            TableCell ta = new TableCell();
            ta.HorizontalAlign = HorizontalAlign.Center;
            btn = new Button();
            btn.Width = 40;
            btn.Height = 30;
            btn.ID = i.ToString() + j.ToString();

            btn.Text = btn.ID.ToString();
            if ((btn.ID == "54") || (btn.ID == "45"))
            {
                btn.BackColor = Color.Black;
                btn.Text = btn.ID.ToString();
                btn.ForeColor = Color.Aqua;
            }
            else if ((btn.ID == "44") || (btn.ID == "55"))
            {
                btn.BackColor = Color.Red;
                btn.Text = btn.ID.ToString();
                btn.ForeColor = Color.Aqua;
            }

            else
            {
                btn.BackColor = Color.Empty;
                btn.ForeColor = Color.Black;
            }
            btn.Click += new EventHandler(Checking);
            ta.Controls.Add(btn);
            myline.Cells.Add(ta);

        }

        tavla.Rows.Add(myline);
    }

    Panel1.Controls.Add(tavla);

}

protected void Checking(object sender, EventArgs e)
{

    Button btn1 = sender as Button; // New button.
    btn1.ID = (int.Parse(btn1.ID) + 1).ToString(); // Button ID += 1
    btn1.BackColor = Color.Red; // Button changes color
    this.Label1.Text = btn1.ID.ToString(); //Label showing the ID of button clicked.

}

最终结果: http://i50.tinypic.com/260frb9.png

【问题讨论】:

    标签: asp.net button dynamic click


    【解决方案1】:

    在检查方法中,你正在修改刚刚点击的按钮的ID,这是错误的。

    你想要的是这样的(在我的脑海中,可能有代码错误):

    protected void Checking(object sender, EventArgs e)
    {
      Button btn1 = sender as Button; // New button.
      string nextId = (int.Parse(btn1.ID) + 1).ToString();
      Button nextBtn = btn1.Parent.FindControl(nextId) as Button;
      //check here to make sure nextBtn is not null :)
      nextBtn.BackColor = Color.Red; // Button changes color
      this.Label1.Text = btn1.ID.ToString(); //Label showing the ID of button clicked.
    
     }
    

    【讨论】:

    • 我也是这么想的。但我不明白为什么第二次点击下一步按钮会改变颜色。只有单击的按钮才能改变颜色。
    • 太棒了,非常感谢!对于 Peri,目标并不是真正为下一个按钮着色,而是为下一个按钮着色是实现目标的一种方式。不管怎样,谢谢 rivarolle,你帮了我很多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    相关资源
    最近更新 更多