GridView: Check if entry has Image or not

Last post 07-09-2008 2:23 PM by KingDario. 5 replies.

Sort Posts:

  • GridView: Check if entry has Image or not

    07-09-2008, 5:49 AM
    • Loading...
    • KingDario
    • Joined on 10-31-2006, 5:54 AM
    • Horgen (Switzerland)
    • Posts 56

    Hi all

    I've a GrivView where I'm showing the entries of a table. each entry MAY (but doesn't have to) have an Image which is read through a handler.
    The problem is that if there's no image for an entry, the cross-icon is shown.

     Is there a property that signals me if the Image control contains data, so that i can hide the Image if it's empty?
    Or does anybody know a better solution

    Filed under: , , ,
  • Re: GridView: Check if entry has Image or not

    07-09-2008, 6:18 AM
    • Loading...
    • Pushkar
    • Joined on 02-17-2006, 7:27 AM
    • http://www.plumtreegroup.net CDIPL - AHD
    • Posts 735

    Hello Mr King..

    Well you can check whether file is exist in its physical location or not...

    if condition satisfied jst set the other image path which indicates No image found..

    Here i sample code...

    You can also hide the Image control...i mean make it visible false...but not looks pretty one.Show like space.. 

    protected void grdtest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
             {
                Image imgCarPic = (Image)e.Row.FindControl("imgCarPic");
                if (string.IsNullOrEmpty(imgCarPic.ImageUrl.ToString()))
                {
                    imgCarPic.ImageUrl = "../images/NoImageSmall.jpg";
                }
                else
                {
                    String strUploadedPicPath = Server.MapPath(imgCarPic.ImageUrl.ToString());
                    if (!System.IO.File.Exists(strUploadedPicPath))
                    {
                        imgCarPic.ImageUrl = "../images/NoImageSmall.jpg";
                    }           
                }
              }
        }

     

    "A conclusion is where you got tired of thinking.
    Be different. Think "

    Remember to click “Mark as Answer” on the post If you get answer from my post(s) !

    Thanks Guys
    ------------
    Pushkar
  • Re: GridView: Check if entry has Image or not

    07-09-2008, 6:21 AM
    • Loading...
    • ziomdk
    • Joined on 06-25-2008, 12:54 PM
    • Denmark
    • Posts 35

    Try to create a templatefield like this:

    <asp:TemplateField ItemStyle-Width=50px >

    <ItemTemplate >

    <img visible="<%# IsEmpty(Eval("Image")) %>" src="ImageURL"/>

    </ItemTemplate>

    </asp:TemplateField>

    Code behind file:

    public bool IsEmpty(Object obj) {
    if (obj.ToString() == "") {
     return true;
    }

    return false;

     

    Karsten Grau Rasmussen


    Please click on 'Mark as Answer' if this post answered your question!
  • Re: GridView: Check if entry has Image or not

    07-09-2008, 6:32 AM
    • Loading...
    • KingDario
    • Joined on 10-31-2006, 5:54 AM
    • Horgen (Switzerland)
    • Posts 56

    Pushkar:

    Hello Mr King..

    Well you can check whether file is exist in its physical location or not...

    if condition satisfied jst set the other image path which indicates No image found..

    Here i sample code...

    You can also hide the Image control...i mean make it visible false...but not looks pretty one.Show like space.. 

    protected void grdtest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
             {
                Image imgCarPic = (Image)e.Row.FindControl("imgCarPic");
                if (string.IsNullOrEmpty(imgCarPic.ImageUrl.ToString()))
                {
                    imgCarPic.ImageUrl = "../images/NoImageSmall.jpg";
                }
                else
                {
                    String strUploadedPicPath = Server.MapPath(imgCarPic.ImageUrl.ToString());
                    if (!System.IO.File.Exists(strUploadedPicPath))
                    {
                        imgCarPic.ImageUrl = "../images/NoImageSmall.jpg";
                    }           
                }
              }
        }

     

     

     thx, but in my case i'm saving the image as byte array in the db, so i can't check on a physical path.

  • Re: GridView: Check if entry has Image or not

    07-09-2008, 7:08 AM
    • Loading...
    • Pushkar
    • Joined on 02-17-2006, 7:27 AM
    • http://www.plumtreegroup.net CDIPL - AHD
    • Posts 735

    Check Binary Image 

    Check Image Information 

    Let me know if any problem. 

     Yeh one more thing when you upload the Image you should check the content length ..

    Check File Size 

    "A conclusion is where you got tired of thinking.
    Be different. Think "

    Remember to click “Mark as Answer” on the post If you get answer from my post(s) !

    Thanks Guys
    ------------
    Pushkar
  • Re: GridView: Check if entry has Image or not

    07-09-2008, 2:23 PM
    Answer
    • Loading...
    • KingDario
    • Joined on 10-31-2006, 5:54 AM
    • Horgen (Switzerland)
    • Posts 56

    ziomdk:
    public bool IsEmpty(Object obj) 
    {
        if (obj != null) 
        {
            return true;
        }
    
        return false;
    } 
    
     

     it worked! but you mustn't use the ToString() method, because if the value is null it will give an error. use simply the following code:

     

Page 1 of 1 (6 items)
Microsoft Communities
Page view counter