View Single Post
  #3 (permalink)  
Old November 30th, 2010, 02:27 PM
tostermann tostermann is offline
Registered User
 
Join Date: Nov 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is my mark for the Markup for the "ManagePhotoAlbum" page:

Code:
<%@ Page Title="Manage Photo Album" Language="C#" MasterPageFile="~/Site/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="EntityDataSource1" InsertItemPosition="LastItem" 
        oniteminserting="ListView1_ItemInserting">
        <InsertItemTemplate>
            <li style="">
                 Description:
                <asp:RequiredFieldValidator ID="regDesc" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage= "Enter a description."></asp:RequiredFieldValidator>
                <asp:TextBox ID="DescriptionTextBox" runat="server" MutiLine="True"
                    Text='<%# Bind("Description") %>' />
                <br />ToolTip:
                <asp:RequiredFieldValidator ID="reqToolTip" ControlToValidate="ToolTipTextBox" runat="server" ErrorMessage= "Enter a tool tip."></asp:RequiredFieldValidator>
                <asp:TextBox ID="ToolTipTextBox" runat="server" Text='<%# Bind("ToolTip") %>' />
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <br />
                <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='<%# Eval("Description") %>' />
                <br />
                ToolTip:
                <asp:Label ID="ToolTipLabel" runat="server" Text='<%# Eval("ToolTip") %>' />
                <asp:Image ID="ImageURL" runat="server" ImageUrl='<%# Eval("ImageURL")%>' />
                <br />
                <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="EntityDataSource1" runat="server" 
        ConnectionString="name=PlanetWroxEntities" 
        DefaultContainerName="PlanetWroxEntities" EnableDelete="True" 
        EnableFlattening="False" EnableInsert="True" EntitySetName="Pictures" 
        Where="it.PhotoAlbum.Id =@photoAlbumId" 
        oninserting="EntityDataSource1_Inserting">
        <WhereParameters>
            <asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId" 
                Type="Int32" />
        </WhereParameters>
    </asp:EntityDataSource>
</asp:Content>
Here is my C# CodeBide for the same file:

[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 EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
{
int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
Picture myPicture = (Picture)e.Entity;
myPicture.PhotoAlbumId = photoAlbumId;

FileUpload FileUpload1 =
(FileUpload)ListView1.InsertItem.FindControl("File Upload1");
string virtualFolder = "~/Site/GigPics";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extention = System.IO.Path.GetExtension(FileUpload1.FileName);

FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder,fileName + extention));
myPicture.ImageURL = virtualFolder + fileName + extention;
}
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 cusValImage =
(CustomValidator)ListView1.InsertItem.FindControl( "cusValImage");
cusValImage.IsValid = false;
e.Cancel = true;
}

}
}

FYI, my GigPics folder is under a Site foler at the base of my website. I know this is a little differ than your setup. I setup the structure wrong at the start of the book. However, I have been able to get the other items to work with small changs to paths without any issues.

Thanks!
Reply With Quote