I was gonna restart ch 14 using the ch. 13 resources cos it would be hard for me to look for where the problem is though any help is good.
As requested i here is the mark up and code behind for both pages.
MAJOR EDIT: I am using ASP.NET 4.5 and i didn't realise this is the 4.0 forum...WHOOPS!
ManagePhotoAlbum.aspx - Mark up
Code:
<%@ Page Title="Manage Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="ManagePhotoAlbum.aspx.cs" Inherits="_ManagePhotoAlbum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="edsListView" InsertItemPosition="LastItem" ItemType="PlanetWroxModel.Picture" OnItemInserting="ListView1_ItemInserting">
<InsertItemTemplate>
<li style="">Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# BindItem.Description %>' />
<asp:RequiredFieldValidator ID="reqDesc" runat="server" ErrorMessage="Enter a description." ControlToValidate="DescriptionTextBox"></asp:RequiredFieldValidator>
<br />ToolTip:
<asp:TextBox ID="ToolTipTextBox" runat="server" Text='<%# BindItem.ToolTip %>' />
<asp:RequiredFieldValidator ID="reqToolTip" runat="server" ErrorMessage="Enter a tool tip." ControlToValidate="ToolTipTextBox"></asp:RequiredFieldValidator>
<br />ImageUrl:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="cusValImage" runat="server" ErrorMessage="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>
<ItemTemplate>
<li style="">
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Item.Description %>' />
<br />
ToolTip:
<asp:Label ID="ToolTipLabel" runat="server" Text='<%# Item.ToolTip %>' />
<br />
<asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Item.ImageUrl %>' />
<br />
<asp:Image ID="Image1" runat="server" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="false" />
</li>
</ItemTemplate>
<LayoutTemplate>
<ul class="ItemContainer">
<li runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>
</asp:ListView>
<asp:EntityDataSource ID="edsListView" runat="server" ConnectionString="name=PlanetWroxEntities" DefaultContainerName="PlanetWroxEntities" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EntitySetName="Pictures" Where="it.PhotoAlbum.Id = @photoAlbumId" OnInserting="edsListView_Inserting">
<WhereParameters>
<asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
</asp:Content>
ManagePhotoAlbum.aspx - Code Behind
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;
public partial class _ManagePhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void edsListView_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/GigPics/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
myPicture.ImageUrl = virtualFolder + fileName + extension;
}
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
if (!FileUpload1.HasFile || !FileUpload1.FileName.ToLower().EndsWith(".jpg"))
{
CustomValidator cusValImage = (CustomValidator)ListView1.InsertItem.FindControl("cusValImage");
cusValImage.IsValid = false;
e.Cancel = true;
}
}
}
NewPhotoAlbum.aspx
Code:
<%@ Page Title="Create New Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="_NewPhotoAlbum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:DetailsView ID="PhotoAlbum" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="EntityDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" InsertVisible="false"/>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=PlanetWroxEntities" DefaultContainerName="PlanetWroxEntities" EnableFlattening="False" EnableInsert="True" EntitySetName="PhotoAlbums" OnInserted="EntityDataSource1_Inserted">
</asp:EntityDataSource>
</asp:Content>
NewPhotoAlbum.aspx - Code Behind
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;
public partial class _NewPhotoAlbum : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{
if (e.Entity != null)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Entity;
Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
myPhotoAlbum.Id.ToString()));
}
}
}