动态创建控件关于控件事件的问题
我的程序是这样的,客户上传图片,我把图片显示在一个panel中,要求点击图片就删除显示这个图片
cs代码
protected void Page_Load(object sender, EventArgs e){
showimg();
}
private void showimg()
{
Panel objpl;
objpl = (Panel)this.DetailsView1.Rows[5].Cells[1].FindControl("Panel1");
objpl.Controls.Clear();
if (ViewState["img"] != null && !String.IsNullOrEmpty(ViewState["img"].ToString()))
{
Label objlb;
String[] arrimg = ((String)ViewState["img"]).Split('|');
ImageButton objibt;
ImageClickEventHandler objsub;
foreach (string s1 in arrimg)
{
objsub = new ImageClickEventHandler(ImageButton1_Click);
objibt = new ImageButton();
objibt.ImageUrl = s1;
//objibt.ID = Guid.NewGuid().ToString();
//objibt.Command += new CommandEventHandler(ImageButton1_command);
objibt.Click += objsub;
//objibt.CommandArgument = s1;
objibt.CssClass = "uploadproduct";
objibt.Style["border"] = "1px solid";
objpl.Controls.Add(objibt);
objlb = new Label();
objlb.Text = " ";
objpl.Controls.Add(objlb);
}
objlb = (Label)this.DetailsView1.Rows[5].Cells[1].FindControl("Label5");
objlb.Text = "提示:点击图片删除!";
}
}
void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton objai = (ImageButton)sender;
Label6.Text = objai.ImageUrl;
// Label6.Text = ;
}
在上传一张图片时候是没问题的,但2张以后我发现,在单击第一张图片时候竟然是除法的最后一张图片的单击,我在objai.ImageUrl中看出来了,请问怎么解决,谢谢
------解决方案--------------------是有点怪,试着给这些按钮一个独特的ID,譬如
int i = 1;
foreach (string s1 in arrimg)
{
...
objibt = new ImageButton();
objibt.ID = "SomeButton" + i.ToString();
i++;
...
}