-2

I have below gridview with an imagefield linked with the UserID number. How can I do if the picture dosnt exist in the specifield older to get an standard alternate picture?

                <ItemTemplate>
                    <asp:Image ID="MainPic" runat="server" 
                        ImageUrl='<%# GetImageUrl(Eval("PerfilId") as string)%>' />
                </ItemTemplate>

..

    protected string GetImageUrl(string dbImgURL)
    {
        if (File.Exists(dbImgURL))
        {
            return dbImgURL;
        }
        else
        {
            return "AdminFotoUser/UserPics/BGP1.png";
        }
    }
RMU
  • 37
  • 3
  • 8

1 Answers1

-1

Well you can check the image path in onRowDataBound event of the GridView and change it to a common image if file doesn't exist. using System.IO;

if (File.Exists(path)) 
                {
                    Console.WriteLine("file exists");
                }

else //Find Control and Bind it to a common image

OR

You could have re-worked your grid a different way: - instead of using an Image Field column, you could have used a template column like this: Code:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Image runat="server" ID="myImg"
            ImageUrl='<%# GetImageUrl(Eval(col2) as string) %>' />
    </ItemTemplate>
</asp:TemplateField>

Then in your code behind, you have create a function to return the correct image url: Code: Updated Ans

//Send Image name from database (DataGrid) as dbImgURL
protected string GetImageUrl(string dbImgURL)
{
   if (File.Exists(dbImgURL)) 
    {
       return dbImgURL;          
    }
   else
    {
       return "Green.gif";
    }
}
panky sharma
  • 2,029
  • 28
  • 45
  • thanks, that was what I was looking for. one mor thing. If the picture existes in the file and I want the gridview to use the existing picture how I will have to replace the >return "Green.gif" – RMU Nov 26 '13 at 18:32
  • Thanks for that, but I still get back only the "Green.gif" and not the "if return". :-) (update my question) – RMU Nov 27 '13 at 13:21
  • @RMU I dont know what do you want to achieve..Its quite simple logic..Simple if else nothing more .... If DataBaseImage Exists Then Use DataBaseImage ........>>ELSE Use Green (N/A) image – panky sharma Nov 27 '13 at 13:35