2

一部の画像が破損していて表示されないため、別の画像を提供しようとしています.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) { }
}
4

5 に答える 5

6

画像の を使用して、onerrorロードされていない場合は別の画像を表示できます。

これは、1 つの img タグを使用した基本的な例です。asp.net では、同様のonerror呼び出し を行うことができます。

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

そしてジャバスクリプト

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の画像ボタン

onerror="imgError(this)"asp.netタグに次のように追加するだけです:

<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") %>'

ここで、画像が存在するかどうかを再確認し、存在しない場合はデフォルトの画像を送信できます。

于 2012-12-31T11:09:19.830 に答える
2
<img src="images.png" onerror="this.onerror=null;this.src='default.png'">
于 2012-12-31T11:21:03.453 に答える
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>
于 2016-03-29T16:16:02.180 に答える
0

imageURL表示するプロパティを設定するdefault imageことをお勧めします。画像がレコードに存在する場合は、そのレコードdatabaseに置き換えimageURLます。それ以外の場合はそのままにしておきます。

于 2012-12-31T11:01:24.590 に答える
0

使うだけonerror event

<img src="image.gif" onerror="alert('The image could not be loaded.')"> 
于 2012-12-31T11:45:26.990 に答える