【问题标题】:OnLoad client event for Image control图像控件的 OnLoad 客户端事件
【发布时间】:2012-05-01 01:24:55
【问题描述】:

我需要将客户端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)" />

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

【问题讨论】:

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


【解决方案1】:
$("img #id").bind("load", function () { $(this).css("visibility", "visible"); });

【讨论】:

  • 太重 - 这将绑定所有图像,并更改页面中的所有图像。
  • 通过编辑您的帖子/答案,您在此处做出了重复的答案。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

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

    从 cmets 编辑

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

    【讨论】:

    • 此图像位于中继器控件中,我该如何添加。我试过它给了我找不到错误控制..
    【解决方案4】:
    $("#imgTopFourImg2").bind("load", function () { $(this).show(); });
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多