Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 动态创建的按钮赢得';在第二次点击之前不要开火_Asp.net_Button_Dynamic_Click - Fatal编程技术网

Asp.net 动态创建的按钮赢得';在第二次点击之前不要开火

Asp.net 动态创建的按钮赢得';在第二次点击之前不要开火,asp.net,button,dynamic,click,Asp.net,Button,Dynamic,Click,我正在动态地创建一个有许多按钮的表。目标是每次我点击一个按钮,旁边的按钮就会改变颜色。然而,每次我点击一个按钮,我点击的那个就会改变它的颜色。只有在第二次单击之后,它旁边的一个才会更改其颜色 我对这方面很陌生,而且我的英语不太好,所以如果你能尽可能简单地解释一下,那就最好了 代码如下: protected void Page_Load(object sender, EventArgs e) { Table tavla = new Table(); //New Table tavl

我正在动态地创建一个有许多按钮的表。目标是每次我点击一个按钮,旁边的按钮就会改变颜色。然而,每次我点击一个按钮,我点击的那个就会改变它的颜色。只有在第二次单击之后,它旁边的一个才会更改其颜色

我对这方面很陌生,而且我的英语不太好,所以如果你能尽可能简单地解释一下,那就最好了

代码如下:

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.

}
受保护的无效页面加载(对象发送方,事件参数e)
{
tavla表=新表();//新表
tavla.GridLines=GridLines.Both;//GridLines
tavla.BorderWidth=4;//BorderWidth
tavla.ID=“tbl1”//表ID
按钮btn=null;//新建按钮

对于检查方法中的(inti=1;i,您正在修改刚刚单击的按钮的ID,这是错误的

你想要的是这样的东西(在我的头顶上,可能有代码错误):


这也是我的想法。但我不明白为什么第二次单击“下一步”按钮会改变颜色。只有单击的按钮才会改变颜色。太棒了,非常感谢!对于Peri来说,目标不是给“下一步”按钮上色,但给“下一步”按钮上色是一种方法。无论如何,谢谢你Rivarle,你帮了我很多忙!。
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.

 }