DEFAULT IMAGE NOT SHOWN IN ASP IMAGEFIELD

Allen Scott

Total Post:46

Points:324
Posted by  Allen Scott
C#  Gridview 
 1134  View(s)
Ratings:
Rate this:

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" />

  1. Chris S

    Post:48

    Points:336
    Re: Default image not shown in ASP ImageField

    <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";
            }
     
        }

      Modified On Apr-09-2018 12:34:18 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!