Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Displaying images in asp.net gridview using imagefield - Part 31

Suggested Videos 
Part 28 - Delete mulitple rows from asp.net gridview
Part 29 - Enhancing gridview using jquery when deleting mulitple rows
Part 30 - Displaying images in asp.net gridview using templatefield



In Part 30 of asp.net gridview tutorial we discussed about displaying images in gridview using templatefiled. Please watch Part 30, before proceeding. 

ImageFiled is designed for the purpose of displaying images in databound controls like GridView and DetailsView. So, it is better to use ImageField over TemplateField



We will be modifying the example we discussed in Part 30, to use ImageField instead of TemplateField. Notice that, in the gridview, TemplateField is replaced with ImageField. 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Gender" HeaderText="Gender" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:ImageField HeaderText="Photo" DataImageUrlField="Photo" 
            ControlStyle-Height="100px" ControlStyle-Width="100px">
        </asp:ImageField>
    </Columns>
</asp:GridView>

Please note that
1. DataImageUrlField of "ImageField" is set to "Photo" property of the Employee object.
2. To control the width and height of the image in gridview, we are using ControlStyle-Height and ControlStyle-Width properties. Both of them are set to 100 pixels.
3. Use AlternateText property, to specify the text that you want to display, if the image is missing.
4. If you want to have different alternate text for each image displayed, use DataAlternateTextField property.
5. If the image path is null, and if you want to have a different image, then use NullImageUrl property.
6. If you want to display alternate text, when photo path is null, then use NullDisplayText property.

2 comments:

It would be great if you can help share these free resources