Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace non-existing image with default image in a formview

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I have a formview:
Code:
<asp:FormView CellPadding="4" ID="fvImg" runat="server" OnDataBinding="fvImg_OnDataBinding" EmptyDataText="There are no items associated with your selected criteria." DataSourceID="PartDetailDataSource">
        <ItemTemplate>
            <tr>
                <td style="width:255px;" rowspan="7" valign="top">
                    <asp:Image ID="partImg" runat="Server" ImageUrl='<%# "/Parts/" + Eval("PART_CODE") + "-0-250x188.jpg" %>' />...

I don't store images or image urls in the DB, I just use the part_code which is part of the image name, to pull the image from a "/Parts/" directory. What I want to do is display a default image, say "NotAvail.gif" if there is no image in the directory for a given part_code.

I can't seem to figure out how to do this. I don't know how to check the directory or do the replace. Can anyone help me with the code behind for this?

Thanks!
 
You will have to use the System.IO namespace and the FileExits() method.
 
I thought I was on the right track with the following example, but I wasn't able to edit it to my needs. It uses the System.IO namespace and File.Exists. I don't know how to make it work in for my formview. This code is unedited as I found it:
Code:
using System.Web.UI.WebControls;
using System.IO;


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    Image CurrentImage = (Image)e.Row.FindControl("imgLogo");
    if (!File.Exists(Server.MapPath(CurrentImage.ImageUrl)))
    {
     // if image not exists, use default image
     CurrentImage.ImageUrl = "~/Images/DefaultImage.gif";
    }
  }
}

Any ideas on how to make this work for my formview based image?
 
Use the databound event of the formview.
Construct the imageURL, then check if the file exits and show the appropriate image.
 
Well, I'm making some progress. What I have now displays the replacement image everytime...so I'm not handling the file exists properly.

Here's what I have:
Code:
protected void fvImg_DataBound(object sender, EventArgs e)
        {
            Image CurrentImage = (Image)fvImg.FindControl("partImg");
            if (!File.Exists(Server.MapPath(CurrentImage.ImageUrl)))
            {
                // if image not exists, use default image
                CurrentImage.ImageUrl = "/Parts/NotAvail.gif";
            }
            
        }

Any ideas?
 
You'll have to trace through the code and see what the mappath is returning and check it against your dir structure.
 
Clarifying my previous post...

Meaning, the /Parts/NotAvail.gif image now displays for every record, even where an image does exist in the directory.

What's wrong with my code? I'm assuming it must have to do with the "if (!File.Exists..." line???

Thanks for any help.
 
You will have to TRACE through your code using the debugger and check what value you are getting for server.mappath
 
Okay, so stepping through the code in Debug, I found that Server.mappath isn't going to work. How can I explicitely define the path if it's say ?
 
Nevermind, I see what you are trying to do.

Like I said:
In the code behind, create your path.

Something like
" + your "PART_CODE") + "-0-250x188.jpg"
Then use that string in the FileExists() method.
 
Okay, so going down the create my own path road, I have this:
Code:
protected void fvImg_DataBound(object sender, EventArgs e)
        {
            ImgPath = "[URL unfurl="true"]http://hcjtest05:4851/Parts/"[/URL] + part_code + "-0-250x188.jpg";

            if (!File.Exists(ImgPath))
            {
                // if image not exists, use default image
                CurrentImage.ImageUrl = "/Parts/NotAvail.gif";
            }
            
        }
How do I get my part_code from the datasourse into this code behind? Then, am I properly handling the if (!File.Exists... part?
 
That last code wasn't quite right, so I'm now trying this...
Code:
protected void fvImg_ItemCreated(object sender, EventArgs e)
        {
            Image CurrentImage = (Image)fvImg.FindControl("partImg");

            Label partCode = (Label)(fvImg.FindControl("lbPartCode"));

            string ImgPath = "[URL unfurl="true"]http://hcjtest05:4851/Parts/"[/URL] + partCode.ToString() + "-0-250x188.jpg";

            if (!File.Exists(ImgPath))
            {
                // if image not exists, use default image
                CurrentImage.ImageUrl = "/Parts/NotAvail.gif";
            }
            
        }
 
Sorry I screwed up in my last post again.

Where are the files(images, being stored?
If they are on the server that has the application, then you can use FileExists.

If you have an image server and you referenc them by using http"\\MyImageServer\somepath\someimage.jpg, then you cannot use FileExists()

In that case you have to use a WebResponse object. This gets more complicated.
 
They're in Sharepoint, and I do have to reference them like "http\\sitename\Parts\", so I bet FileExists is out.

 
The asp.net route was proving much too difficult and time consuming for such a seemingly simple thing.

So I took a look at my options in javascript and was amazed to find such a simple solution.

So here it is for everyone's future reference...

Using the javascript "onError" method, you replace the src of the image tag with your default replacement image.

onError is triggered when the browser doesn't find the image it's looking for.

Here's the working example:
<asp:Image ID="partImg" runat="Server" onError="this.onerror=null;this.src='/Parts/NotAvail.gif';" ImageUrl='<%# "/Parts/" + Eval("PART_CODE") + "-0-250x188.jpg" %>' />

See how simple this is:
onError="this.onerror=null;this.src='/Parts/NotAvail.gif';"

Hopefully this will help others along the way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top