2012-04-19 33 views
1

我需要綁定客戶端的onLoad事件與ASP.Net圖像控件。我試了很長時間沒有成功。圖像控件的OnLoad客戶端事件

功能名稱onload="onLoadFunction(this)"

腳本:

function onLoadFunction(img) { 
    $(img).css("visibility", "visible"); // Using jQuery 
    // img.style.visibility = "visible"; // Using just javascript. 
} 

標記:

<asp:Image ID="imgTopFourImg2" runat="server" width="170px" height="112px" CssClass="ArticleImgHP" border="0" ImageUrl="hello.jpg" OnLoad="onLoadFunction(this)" /> 

它不是爲我工作,我將不勝感激,如果有人能幫助我與此有關。

+0

你在Firebug中看到什麼? – SLaks 2012-04-19 13:57:58

+0

ArticleImgHP是什麼? – Aristos 2012-04-19 14:01:50

+0

這實際上與我以前的問題有關http://stackoverflow.com/questions/10227254/image-progress-bar-for-images-being-downloaded-in-website-not-working-with-aprop/10228540#comment13141900_10228540 – Learning 2012-04-22 04:12:56

回答

2

onload屬性用於添加的事件處理程序的加載事件,這是一個服務器端事件,而不是客戶端。

如果你想創建生成的圖像元素的onload屬性,則需要使用屬性集合從評論

imgTopFourImg2.Attributes["onload"] = "onLoadFunction(this)"; 

EDIT由於圖像是一箇中繼器的項目裏面,這不是在後面的代碼中可用。處理ItemDataBound事件:

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { 

      // This event is raised for the header, the footer, separators, and items. 

      // Execute the following logic for Items and Alternating Items. 
      if (e.Item.ItemType == ListItemType.Item 
        || e.Item.ItemType == ListItemType.AlternatingItem) 
      { 

       var imgTopFourImg2 = e.Item.FindControl("imgTopFourImg2") as Image; 
       if (imgTopFourImg2 != null) 
        imgTopFourImg2.Attributes["onload"] = "onLoadFunction(this)"; 
      } 
      } 
     } 
+0

此圖片位於中繼器控件中,我如何爲此添加內容。我試過它給了我錯誤控制沒有找到.. – Learning 2012-04-22 07:04:28

+0

我做了一個更新 – 2012-04-22 08:30:57

0
$("img #id").bind("load", function() { $(this).css("visibility", "visible"); }); 
+0

太重了 - 這將綁定所有圖像,並更改頁面中的所有圖像。 – Aristos 2012-04-19 14:02:46

+0

通過編輯你的文章/答案,你在這裏做出了重複的答案。 – 2012-04-19 14:11:47

3

$("img #xyz").bind("load", function() { $(this).css("visibility", "visible"); });

1
$("#imgTopFourImg2").bind("load", function() { $(this).show(); }); 

值得期待到show()hide()方法,因爲你已經使用jQuery。