2016-08-24 43 views
1

我想從後面的代碼爲每個ImageButton分配事件,但我找不到如何編寫正確的事件。將事件分配給ImageButton.Click從後面的代碼

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

您的事件處理方法簽名是錯誤的,你需要從的射擊事件的按鈕得到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; 
} 
相關問題