views:

34

answers:

1

I am trying to implement an image gallery in ASP.NET./c#

I am displaying 6 thumbnails on my page. To display this I have decided to use ListView with an ItemTemplate containing an ImageButton control in it for each thumbnail that I would be displaying. I have a few questions:

  1. Am I choosing the right controls? (there are options like DataList, repeater, etc)
  2. I am reading from a sql server 2008 database that has image as varbinary type field. If I want to display this binary data as images in my thumnails then how should I go about this?
  3. Ultimately I want to be able to display a large image when a thumbnail is clicked upon. How will I copy the URL of the thumbnail that is clicked?
  4. Does anyone have a reference/demo/sample that I could use as a guide?

PLEASE remember that images are "binary" data in sql server and NOT files on a file system.

I can write the sql logic to pick the correct images in my sqldatareader or may be use a dataset instead. I want to proceed with my asp.net controls and its code behind where I am having these initial design issues like what controls to be used, how to add dynamically, etc.

Thanks.

A: 
  1. You may also use Repeater but ListView is more flexible from layout perspective (not to mention paging support). However, ImageButton for displaying thumbnail is not a good choice, I will suggest using image control (or img tag) embedded in hyperlink (or achor tag) for the same.

  2. You have to save image data from database to file system and then give those as URLs. Or you can write an http handler (aspx/ashx) that will stream image data from database and you will use handler path as URL for images. In either approach, you should use cache headers for better performance. From all things considered, I would tend to go with first approach for its simplicity.

  3. Generally, you use markup such as

     <a href="actual image URL" target="_blank" class="aLink">
       <img src="thumbnail URL" alt="Click to see image" />
     </a>
    

    So anchor tag has path to actual Image url. Now, you have lot of java-script libraries (collectively called as lightbox) that you can use to show image gallary - see http://leandrovieira.com/projects/jquery/lightbox/, http://colorpowered.com/colorbox/, http://fancybox.net/ etc.

  4. Above libraries will have demo/examples for generating gallery from client side. All you have to do from server side is to generate mark-up (html) illustrated in example - which is really simple using repeater/listview and data-binding syntax.

VinayC
Yess I thought of doing this earlier. But I do not know the "thumbnail URL" inside the img tag right. I mean there could be 0 or there could be 100 images pulled from the database as binary data. I need to find a way to bind this img control (or Listview control correct me if I am wrong) to a datasource (sqlreader, datatable, List<>, need to help here to decide which one) to be able to dusplay these images as thumbnails along with the text (image name, description associated, etc)
VP
can you manually add image items to a listview control in code behind ? is there an example for this?
VP
@VP, see my second point - you cannot bind binary data directly with image control. You need to save it in file system. Now the (temp) folder where you will save those images can be exposed as virtual directory under your web site and that will give you your thumbnail urls. Other option can be serve image thumbnails them on demand using ashx/aspx.axd (see one sample code: http://weblogs.asp.net/cazzu/archive/2003/08/27/25568.aspx)
VinayC
Ok. I actually wrote an httphandler to pull the image from my sql server database and display it. Also can you please give me an idea of how I would include a pager for this kind of a display. I have like 100s of images loading on the same page. I want to be able to load only 4 at a time on a page and then provide links like "1 of 10 Next-> " at the bottom. I hope that my question is clear. Also there is no row/column in my database that keeps this count. I have do all the computation in my code behind to design this. Any detailed help/links would be great. Thanks
VP
@VP, Use DataPager control with ListView. It supports customizing the pager layout and also allows custom paging (where you will only fetch a page of data (i.e. say only 4 records at a time). Here are couple of links that should get u started: http://mattberseth.com/blog/2007/12/data_navigation_with_the_listv.html, http://thibautvs.com/blog/?p=718
VinayC