asp.net中的替代图像显示

时间:2012-12-31 10:53:00

标签: c# javascript asp.net datalist

我正在尝试提供替代图像,因为有些图像已损坏并且无法显示,我尝试使用js但没有成功,因此需要有关如何执行此操作的帮助和建议。场景是在页面加载学生的信息绑定到DataList,并在此基础上图像是由GetImage类从另一个表拉出但问题是一些图像已损坏所以我想显示另一个虚拟图像。我使用CSS更改了BG图像,但不像预期的那样。

    <asp:DataList ID="DataList1" CssClass="slider" r RepeatColumns="6" RepeatDirection="Vertical" runat="server" OnEditCommand="DataList1_EditCommand" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
    <ul>
    <li><a class="info" href="#">
        <asp:ImageButton CssClass="imagetest" AlternateText=" " ID="Image1" name="mybutton" runat="server" Width="140" Height="140" CommandName="image" OnLoad="changeImage();" CommandArgument="image" CausesValidation="false" ImageUrl='<%# "GetImage.aspx?source=" + Eval("city") +"&RollNo="+ Eval("RollNo") +"&state="+ Eval("state")+"&fname="+Eval("FirstName") +"&lname=" + Eval("LastName") %>'
        </li>
    </ul>
</ItemTemplate>
</asp:DataList>


protected void Page_Load(object sender, EventArgs e)
{       
    string city = Request.QueryString["city"];
    string RollNo = Request.QueryString["RollNo"];
    string state = Request.QueryString["state"];
    string fname = Request.QueryString["fname"];
    string lname = Request.QueryString["lname"];
    DataAccess dao = new DataAccess();
    dao.openConnection();
    byte[] arr = dao.GetPicture(city, RollNo, state, fname, lname);
    //Response.BinaryWrite(arr);
    try
    {    
        Response.BinaryWrite(arr);
    }    
    catch (Exception) { }
}

5 个答案:

答案 0 :(得分:5)

如果未加载,您可以使用图像的onerror并显示替代图像。

以下是一个带有一个img标记的基本示例,在asp.net上,您可以进行类似的onerror调用

<img id="one" src="image.gif" onerror="imgError(this)">​

和javascript

function imgError(me)
{
   // place here the alternative image
   var AlterNativeImg = "/images/emptyimage.gif";

   // to avoid the case that even the alternative fails        
   if(AlterNativeImg != me.src)
     me.src = AlterNativeImg;
}

并且直播:http://jsfiddle.net/DxN69/2/ 还有http://jsfiddle.net/DxN69/3/

和最终:http://jsfiddle.net/DxN69/4/

在asp.net图像按钮

您可以在asp.net标记上简单地添加onerror="imgError(this)"

<asp:ImageButton ID="ImageButton1" runat="server" onclick="ImageButton1_Click" ImageUrl="image.jpg" onerror="imgError(this)" />

即使图像按钮呈现为input,最终结果也是相同的。

从后面的代码发送图像

在您上次更新后明确表示您在尝试将页面作为图片发送时遇到了很大的错误,而您甚至没有更改它的ContentType。所以使用handler,例如创建一个名为loadImage.ashx的新处理程序,并将代码编写为:

   public void ProcessRequest(HttpContext context)
    {
      // this is a header that you can get when you read the image
      context.Response.ContentType = "image/jpeg";
      // cache the image - 24h example
      context.Response.Cache.SetExpires(DateTime.Now.AddHours(24));
      context.Response.Cache.SetMaxAge(new TimeSpan(24, 0, 0));
      // render direct
      context.Response.BufferOutput = false;

      // load here the image as you do
      ....

      // the size of the image
      context.Response.AddHeader("Content-Length", imageData.Length.ToString());
      // and send it to browser
      context.Response.OutputStream.Write(imageData, 0, imageData.Length);
    }

您的电话将是:

<asp:ImageButton CssClass="imagetest" AlternateText=" " ID="Image1" name="mybutton" runat="server" Width="140" Height="140" CommandName="image" OnLoad="changeImage();" CommandArgument="image" CausesValidation="false" ImageUrl='<%# "loadImage.ashx?source=" + Eval("city") +"&RollNo="+ Eval("RollNo") +"&state="+ Eval("state")+"&fname="+Eval("FirstName") +"&lname=" + Eval("LastName") %>'

现在,您可以仔细检查图像是否存在,如果不存在则发送一些默认图像。

答案 1 :(得分:2)

<img src="images.png" onerror="this.onerror=null;this.src='default.png'">

答案 2 :(得分:0)

我希望为您要显示的imageURL设置default image属性。如果图像存在于database记录中,则将imageURL替换为该记录,否则保持原样。

答案 3 :(得分:0)

只需使用onerror event

即可
<img src="image.gif" onerror="alert('The image could not be loaded.')"> 

答案 4 :(得分:0)

不是创建服务器端图像按钮,而是为什么不创建带链接的html图像,而是用作图像按钮。

<a class="info" href="#">
    <img src="<%# GetImage.aspx?source=" + Eval("city") +"&RollNo="+ Eval("RollNo") +"&state="+ Eval("state")+"&fname="+Eval("FirstName") +"&lname=" + Eval("LastName") %>" onerror="this.src='<%=Page.ResolveClientUrl("~/images/emptyimage.gif") %>'" alt="" class="imagetest" />
</a>