i’m creating a shopping website. i have a gridview that is bound to an objectdatasource control for administration tasks. this grid is responsible for showing the items table in a sql server database. each item also might or might not have a picture. so when this grid is shown to the admin i want a small thumbnail of the associated image to be available for each row. here is the gridview code:
<asp:GridView ID="gridview1" runat="server"
DataSourceID="objDataSource" DataKeyNames="item_id">
<Columns>
<asp:BoundField DataField="item_category" Visible="false" />
<asp:BoundField HtmlEncode="true" DataField="item_name"
HeaderText="Item Name" />
<asp:BoundField HtmlEncode="true" NullDisplayText="Not Set"
DataField="item_desc" HeaderText="Item Description" />
<asp:TemplateField HeaderText="Item Picture"
ItemStyle-Width="40px" ItemStyle-Height="40px">
<ItemTemplate>
<!-- here should be the image -->
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowSelectButton="True"
HeaderText="Selecting" />
</Columns>
</asp:GridView>
i’m only aware of two options when it comes to showing image in the browsers:
- save a bitmap on the response stream. this will not be inside of gridview when it renders to client(correct?).
- save a bitmap on hard drive after the select statement and use an image control inside of template then assign the ImageUrl attribute dynamically.
second option is too expensive because it needs writing and reading on a hard drive for each image(or datarow). so how can i do this? is it even possible?
ps: ajax is not an option.
Create an ASP.NET Web Handler (.ashx file) that handles your image, pass the id of the image to your gridview and create a “link” to that image handler that will either display the image if it exists, or possibly display a ‘no image available’ image
Here is a quick tutorial on ASHX files: http://www.dotnetperls.com/ashx
I can post additional code if you need it