Default image not shown in ASP ImageField

In my asp.net gridview there's an ASP ImageField,When image is not shown or broken I need to show the Default Image, here I use NullImageUrl for this but this won't work.

My Code:

<asp:ImageField DataImageUrlField="FilePath" ControlStyle-Height="50" ControlStyle-Width="50" HeaderText="Image Preview" NullImageUrl="~/images/Defaultimg.png" />

Last updated:1/29/2015 10:41:49 PM

1 Answers

Anonymous User
Anonymous User

<asp:Image class='fbimgsize' ImageUrl='<%#FormatURL(Convert.ToString(DataBinder.Eval(Container.DataItem,
'picture'))) %>'
  runat='server' ID='imgUser' />
public string FormatURL(string img)
    {
        try
        {
            if (Path.GetExtension(img).Equals('.jpg',StringComparison.InvariantCultureIgnoreCase))
            {
                return img;
            }
            else
            {
                return 'images/noimage.jpg';
            }
        }
        catch (Exception)
        {
           return 'images/noimage.jpg';
        }
 
    }

Answer