The default image is displayed if the image does not exist on the Asp.net page.

Source: Internet
Author: User

In Asp.net development, if you want to display the default image when the image cannot be found, we have the following methods:

1. JS

You can obtain the object of each image tag on the page. Then set its src attribute to OK.

Code<SCRIPT>

Function window. onload () {var IMG = document. getelementbyid ("form1 "). getelementsbytagname ("IMG") for (VAR I = 0; I

</Scirpt>

2. webconfig Configuration

Configuring webconfig can easily and practically solve some difficult problems. First, the webconfig configuration file must be loaded for each solution running. We can write a public class to inherit ihttphandler. Configure

(1). Class Method

Code public imagemanager () {// todo: add the constructor logic here //} private const string defaultimage_url = "Default. jpg ";

Public bool isreusable {get {return false ;}}

 

Public void processrequest (httpcontext context) {// obtain the different physical address of each image in the request string imgphysicalpath = context. Request. physicalpath; system. Drawing. Image image = NULL;

If (file. exists (imgphysicalpath) {// null

} Else {// if the image does not exist, place the default image = system. drawing. image. fromfile (context. server. mappath (defaultimage_url);} // sets the output type context. response. contenttype = "image/JPEG"; // Save the image to the output stream. save (context. response. outputstream, system. drawing. imaging. imageformat. JPEG); image. dispose ();

}

(2). webconfig Configuration

<Add verb = "*" Path = "path/*. jpg" type = "public class name"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.