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);
}
{
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);
}
解决方案 »
- 如何设置窗体高度
- 急求asp.net下实现web操作office包括手写输入修改文档的资料
- 怎么在aspx页面 如(login.aspx)引用命名空间(System.data.sqlclient)
- 求思路!动态加载子程序
- jmail发送邮件的问题(多多帮忙)
- 为什么VS.NET里绘制表格…………
- 不同页面的数据传递以及word文件如何以只读的页面形式显示???
- 如何在DATAGRID中增加,删除一行?
- 高手请进 高分求教
- 100分求HtmlTable的添加行和删除行的解决方法!
- 怎样才能得到DataGrid中选中的CheckBox的值
- 我把一个网页文件转到visual studio .net中,出现错误,请看这是什么原因
ib.ID....//属性设定
ImageButton ib = new ImageButton();
执行一次就创建了一个新的对象,上一轮循环的那一个ImageButton并没有消亡,它已经被加入到DataGrid中,而ib这个变量又指向了新的对象。C#里面有引用类型和值类型,像这种就是引用类型,重新new之后,只是说ib指向了新的对象,原来的对象并没有被消亡,只有一个对象没有被引用了才会被GC回收,这个例子中,DataGrid1.Items[i].Cells[6].Controls[0]引用了ImageButton,所以每一次创建的并没有消失。
而且,你是在循环中创建的
正好可以取得循环次数中的i值
来给每次new出来的imageButton设定ID值
例如
ImageButton ib = new ImageButton();
ib.ID = "imageButton_"+i.ToString();
...
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);
}
给分解帖。