asp.net中的替代图像显示

Was*_*asi 2 javascript c# 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) { }
}
Run Code Online (Sandbox Code Playgroud)

Ari*_*tos 5

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

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

<img id="one" src="image.gif" onerror="imgError(this)">?
Run Code Online (Sandbox Code Playgroud)

和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;
}
Run Code Online (Sandbox Code Playgroud)

和生活:http://jsfiddle.net/DxN69/2/ 以及http://jsfiddle.net/DxN69/3/

最后:http://jsfiddle.net/DxN69/4/

在asp.net图像按钮上

你可以简单地onerror="imgError(this)"在你的asp.net标签上添加:

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

即使图像按钮被渲染为input最终结果也是如此.

图像从后面的代码发送

在您的上一次更新后,您明确表示您在尝试将页面作为图像发送时犯了一个大错误,而您甚至没有更改ContentType它.所以使用a 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);
    }
Run Code Online (Sandbox Code Playgroud)

你的电话会是这样的:

<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") %>'
Run Code Online (Sandbox Code Playgroud)

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