for(int i=0;i<DataGrid1.Items.Count;i++)
{
  ImageButton1 = new ImageButton();
  ImageButton.ID = "";//你可以自己赋ID,也可以不赋,页面框架会自动分配
  //取得ImageButtn.ImageUrl的值
  ImageButton1.ImageUrl=this.DataGrid1.Items[i].Cells[7].Text;
  this.DataGrid1.Items[i].Cells[6].Controls.Add(ImageButton1);
}

解决方案 »

  1.   

    ImageButton imgbtn = new ImageButton();
      

  2.   

    ImageButton ib = new ImageButton();
    ib.ID....//属性设定
      

  3.   

    我的意思是通过循环创建多个imagebutton,也就是说ImageButton ib = new ImageButton();中ib是变量。
      

  4.   

    这样就可以啊,这就是创建了多个ImageButtonnew 操作符是创建一个新的对象
    ImageButton ib = new ImageButton();
    执行一次就创建了一个新的对象,上一轮循环的那一个ImageButton并没有消亡,它已经被加入到DataGrid中,而ib这个变量又指向了新的对象。C#里面有引用类型和值类型,像这种就是引用类型,重新new之后,只是说ib指向了新的对象,原来的对象并没有被消亡,只有一个对象没有被引用了才会被GC回收,这个例子中,DataGrid1.Items[i].Cells[6].Controls[0]引用了ImageButton,所以每一次创建的并没有消失。
      

  5.   

    的确没有影响啊
    而且,你是在循环中创建的
    正好可以取得循环次数中的i值
    来给每次new出来的imageButton设定ID值
    例如
    ImageButton ib = new ImageButton();
    ib.ID = "imageButton_"+i.ToString();
    ...
    ok~~~~~~~~~
      

  6.   

    OK,非常感谢各位的鼎力相助。实现代码:
    for(int i=0;i<DataGrid1.Items.Count;i++)
      {
        ImageButton IB= new ImageButton();
        IB.Width=90;
        IB.Height =80;
        IB.ImageUrl=this.DataGrid1.Items[i].Cells[7].Text;
        this.DataGrid1.Items[i].Cells[6].Controls.Add(IB);
    }
    给分解帖。