Pictures in Photo Album wont display
Imar,
I have been following your steps in building the photoalbum and have managed to successfully implement the photo album functionality completely in a previous demo.
I restarted again, making it into my final year project community website, but for some reason, this time, my images wont load in the listview.
The images are certainly in the database. They get stored physically into the folder ~/ClubPhotos/". The photos ref is stored in the database column "ImageUrl" e.g ~/ClubPhotos/61e1773e-b313-465e-a8e7-5bbd00d55e6f.jpg.
When viewing a photo album using the query string, all my photo data gets displayed except for the image.
I'v built this three times from scratch now, any idea's why the image might not be displayed?
Here's my code from the Page and Code Behind. The database and LINK data classes are exactly the same as in the book.
Cheers,
Robin
<h1>
Manage Photo Album</h1>
<p>
</p>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
DataSourceID="LinqDataSource1" InsertItemPosition="LastItem"
oniteminserting="ListView1_ItemInserting">
<ItemTemplate>
<li style="">
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
Tooltip:
<asp:Label ID="TooltipLabel" runat="server" Text='<%# Eval("Tooltip") %>' />
<br />
<asp:Image ID="Image1" runat="server" ImageUrl='<% Eval("ImageUrl") %>'/>
<br />
<asp:Button ID="DeleteButton" runat="server" CausesValidation="false" CommandName="Delete"
Text="Delete" />
</li>
</ItemTemplate>
<InsertItemTemplate>
<li style="">Description:
<asp:TextBox ID="DescriptionTextBox" TextMode="MultiLine" runat="server"
Text='<%# Bind("Description") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Please enter a description"></asp:RequiredFieldValidator>
<br />
Tooltip:
<asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TooltipTextBox" runat="server" ErrorMessage="Please enter a tooltip"></asp:RequiredFieldValidator>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select a valid .jpg file"></asp:CustomValidator>
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" CausesValidation="false" />
</li>
</InsertItemTemplate>
<LayoutTemplate>
<ul class="itemContainer">
<li ID="itemPlaceholder" runat="server" />
</ul>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<p>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext" EnableDelete="True"
EnableInsert="True" OrderBy="Id desc" TableName="Pictures"
Where="PhotoAlbumId == @PhotoAlbumId"
oninserting="LinqDataSource1_Inserting">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="-1" Name="PhotoAlbumId"
QueryStringField="PhotoAlbumId" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
</p>
protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
string virtualFolder = "~/ClubPhotos/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator CustomValidator1 =
(CustomValidator)ListView1.InsertItem.FindControl( "CustomValidator1");
CustomValidator1.IsValid = false;
e.Cancel = true;
}
}
|