将事件分配给ImageButton.Click从后面的代码

时间:2016-08-24 14:23:56

标签: c# asp.net events event-handling

我想从后面的代码中为每个ImageButton分配事件,但我无法找到如何编写正确的事件。

foreach (string one in urls)
{
    ImageButton temIBTN = new ImageButton();
    temIBTN.Attributes.Add("Width","265px");
    temIBTN.Attributes.Add("Width", "144px");
    temIBTN.ImageUrl = one;
    temIBTN.Click += 
     new EventHandler(setBigPic(sender, e, one));//<---don't know how...
}



protected void setBigPic(object sender, ImageClickEventArgs e,string url)
{

    img_Big.ImageUrl = url;

}

1 个答案:

答案 0 :(得分:1)

事件处理程序的方法签名错误,您需要从触发事件的按钮获取ImageUrl。这应该这样做:

foreach (string one in urls)
{
    ImageButton temIBTN = new ImageButton();
    temIBTN.Attributes.Add("Width","265px");
    temIBTN.Attributes.Add("Width", "144px");
    temIBTN.ImageUrl = one;
    temIBTN.Click += setBigPic;
}

protected void setBigPic(object sender, ImageClickEventArgs e) 
{
   img_Big.ImageUrl = ((ImageButton)sender).ImageUrl;
}