用于Image控件的OnLoad客户端事件

时间:2012-04-19 13:55:37

标签: c# javascript asp.net .net onload

我需要将客户端onLoad事件与ASP.Net Image控件绑定。我已经尝试了很长时间但没有成功。

功能名称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)" />

这对我不起作用如果有人可以帮助我,我将不胜感激。

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

OnLoad属性用于向事件处理程序添加Load事件,该事件是服务器端事件,而不是客户端事件。

如果要创建生成的图像元素的onload属性,则需要使用Attributes集合

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

来自评论的编辑

由于图像位于转发器项目内,因此在后面的代码中不可用。处理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)";
             }
          }
       }  

答案 2 :(得分:1)

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

值得研究show()hide()方法,因为您已经在使用jQuery。

答案 3 :(得分:0)

$("img #id").bind("load", function () { $(this).css("visibility", "visible"); });
相关问题