Displaying Images dynamically in GridView

  • Thread starter Thread starter King Coffee
  • Start date Start date
K

King Coffee

Hi,

The following is the code snippet I'm trying to develop. The select method
GetPics returns a collection (filtered by Request["AptID"]) of image
filenames from a DB. The primary field returned by GetPics is "PicID". I
want the GridView to display the images per rowset filename DB field.

<asp:GridView ID="GridView1" Runat="server" DataSourceID="ObjectDataSource1"
DataKeyNames="PicID" onrowdatabound="GridView1_RowDataBound" >
<Columns>
<asp:templatefield HeaderText="Photo">
<itemtemplate>
<asp:Image ID="Image1" runat="server" />
</itemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1"
TypeName=".Pics"
DataObjectTypeName="Pics"
SelectMethod="GetPics"
Runat="server" >
<SelectParameters>
<asp:QueryStringParameter QueryStringField="AptID" Type="Int32"
Name="AptId" />
</SelectParameters>
</asp:ObjectDataSource>
_________________________________________________________________________
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = e.Row.FindControl("Image1") as Image;

img.ImageUrl = "~/Gallery/4/Thumbs/" + ???;
}
}

Thanks,
King
 
custom bind the image with a function...

take a look at this example:

http://www.pluralsight.com/community/blogs/fritz/archive/2005/12/16/17507.aspx

here is an example I am using with an image control
Take a look at the databinding for the "ImageUrl" and the function inbehind
in the vb code.
In my datatable, i store just the jpg filename "ImageName".

<dxe:ASPxImage ID="aspximageDesignImage" runat="server" Height="150px"
Width="125px" AlternateText='<%#
Eval("ImageName") %>'
ImageUrl='<%#
ImagePathUrl(DataBinder.Eval(Container.DataItem, "ImageName")) %>'
ToolTip='<%# Eval("ItemDescription") %>'>
</dxe:ASPxImage>


Function ImagePathUrl(ByVal ImageName As String)
DesignBibImageLocation = "ProductImages/" & ImageName.Trim
Return DesignBibImageLocation
End Function

Hope this helps.

Miro
 
Back
Top