Solving image not found problem in asp.net gridview, defining a default image to user profile


To show default image only when image from data source is not found you need to use RowDataBound event of GridView control, with code like this:

[ C# ]

// We need these namespaces
using System.Web.UI.WebControls;
using System.IO;

/// <summary>
/// Check if current image exists and correct ImageUrl
/// property if image not found
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
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”;
}
}
}

[ VB.NET ]

‘ We need this namespaces
Imports System.Web.UI.WebControls
Imports System.IO

”’ <summary>
”’ Check if current image exists and correct ImageUrl
”’ property if image not found
”’ </summary>
”’ <param name=”sender”></param>
”’ <param name=”e”></param>
”’ <remarks></remarks>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim CurrentImage As Image = e.Row.FindControl(“imgLogo”)
If Not File.Exists(Server.MapPath(CurrentImage.ImageUrl)) Then
‘ if image not exists, use default image
CurrentImage.ImageUrl = “~/Images/DefaultImage.gif”
End If
End If
End Sub

One thought on “Solving image not found problem in asp.net gridview, defining a default image to user profile

  1. The adage ”Knowledge is Power” is truer nowhere than in the field of PC security.
    That’s why you really need a Wi-Fi security to protect your privacy. Hackers generally have handles or nicknames they use when communicating.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s