Database driven images in ASP.NET GridView

If you want to show images of products, members, or anything else, one method is to store the image filename in a database field and then add an imagefield column type to an ASP.NET gridview. Here are the steps:

1. Let's assume you have a products table in an Access database (the step are almost identical for MSSQL) with a column called product_image that stores image filenames such as product1.jpg.

2. Let's also assume that you have saved all of your product images to a folder called images in your web site.

3. In Visual Web Developer or Visual Studio, create a GridView based on the products table in the normal way.

4. From GridView Tasks, Select Add New Column

5. For Field Type, select ImageField.

6. Type in a Header if you want.

7. For the data field, type in product_image (or whatever you called your images field in the database).

8. For the URL format string, type in images/{0}

In step 8, images refers to the folder in your web site where you saved all of your images.

When you view your web page, the {0} is replaced by the relevant image filename and then converted into an img HTML tag so that it displays as an image.

Published Thursday, March 08, 2007 8:35 AM by gcastner
Filed under: , ,

Comments

No Comments