Friday, May 29, 2009

Rowbound Property for Gridview Control

This can be a simple tutorial but I think it is useful for beginners. You can use this code snippet to dynamically change the value of an ImageButton while the row is being generated. It is useful in cases where we are listing Images of a product and we have to add a noimage pic ,if there is no image available.

protected void gvLastUpdated_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton imgButton = (ImageButton)e.Row.FindControl("ImageButton1");

if (imgButton.ImageUrl.ToString().Trim() == "")
{
imgButton.ImageUrl = "~\\uploads\\noimage.jpg";
}
else
{
imgButton.ImageUrl = "~\\uploads\\" + imgButton.ImageUrl.ToString();

}
}
}
catch (Exception e1)
{
lblMessage.Text = (e1.ToString());
}
}

with Regards,
Praveen
http://www.linesandgraphs.com

No comments:

Post a Comment