2012-04-19 10 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

+0

ArticleImgHPとは何ですか? – Aristos

+0

これは実際に私の以前の質問に関連していますhttp://stackoverflow.com/questions/10227254/image-progress-bar-for-images-being-downloaded-in-website-not-working-with-aprop/10228540#comment13141900_10228540 – Learning

答えて

2

onload属性は、イベントハンドラにサーバー側のイベントではなく、クライアント側でLoadイベントを追加するために使用されます。コメント

から

あなたが生成された画像要素のonloadの属性を作成したい場合は、属性のコレクションを使用する必要が

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

EDIT画像はこれではないリピーターアイテムの内側にあるので、コードの背後にあります。あなたは既にjQueryのを使用しているので、それは、show()hide()方法に探して価値が

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

+0

私は更新を行った –

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

重すぎる - これはすべての画像をバインドし、ページ内のすべての画像を変更します。 – Aristos

+0

あなたの投稿/回答を編集することで、ここで重複した回答ができました。 –

3

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

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

ItemDataBoundイベントを処理します。