using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Admin_EditSelector_Photos : WebBlocks.AdministrativePage { private const string TinyDir = "~/ProductPhotos/Tiny/"; private const string SmallDir = "~/ProductPhotos/Small/"; private const string MediumDir = "~/ProductPhotos/Medium/"; private const string LargeDir = "~/ProductPhotos/Large/"; public int SelectorID { get { return (ViewState["SelectorID"] == null ? 0 : (int)ViewState["SelectorID"]); } set { ViewState["SelectorID"] = value; } } public int PhotoSetID { get { return (ViewState["PhotoSetID"] == null ? 0 : (int)ViewState["PhotoSetID"]); } set { ViewState["PhotoSetID"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request["SelectorID"] != null) SelectorID = Int32.Parse(Request["SelectorID"]); BindPhotos(); } } private void BindPhotos() { DataTable dt = Database.PhotoSets_GetBySelectorID(SelectorID); if (dt.Rows.Count > 0) imgPrimary.ImageUrl = "../LiveImage.ashx?preset=medium&img=ProductPhotos/" + (string)dt.Rows[0]["Filename"]; else imgPrimary.ImageUrl = "../LiveImage.ashx?preset=medium&img=ProductPhotos/noPhoto.gif"; dlPhotos.DataSource = dt; dlPhotos.DataBind(); btnDelete.Visible = (dt.Rows.Count > 0); btnSetPrimary.Visible = (dt.Rows.Count > 0); } private void BindEdit() { DataRow dr = Database.PhotoSets_Get(PhotoSetID); FileSingle.Filename = (string)dr["Filename"]; litPhotoSetID.Text = dr["PhotoSetID"].ToString(); imgPrimary.ImageUrl = "../LiveImage.ashx?preset=medium&img=ProductPhotos/" + (string)dr["Filename"]; txtCaption.Text = (string)dr["Caption"]; } protected void btnSave_Click(object sender, EventArgs e) { if (Request["Wizard"] != null) { Response.Redirect("EditSelector_ProductAttributes.aspx?Wizard=1&SelectorID=" + SelectorID); } else Response.Redirect("EditSelector_ProductAttributes.aspx?SelectorID=" + SelectorID); } protected void btnSetPrimary_Click(object sender, EventArgs e) { int SelectedPhotoSetID = 0; foreach (DataListItem dli in dlPhotos.Items) { RadioButton rbSelect = (RadioButton)dli.Controls[3]; if (rbSelect.Checked) SelectedPhotoSetID = (int)dlPhotos.DataKeys[dli.ItemIndex]; } if (SelectedPhotoSetID > 0) { Database.PhotoSets_SetDefaultPhoto(SelectorID, SelectedPhotoSetID); BindPhotos(); } } protected void btnDelete_Click(object sender, EventArgs e) { int SelectedPhotoSetID = 0; foreach (DataListItem dli in dlPhotos.Items) { RadioButton rbSelect = (RadioButton)dli.Controls[3]; if (rbSelect.Checked) SelectedPhotoSetID = (int)dlPhotos.DataKeys[dli.ItemIndex]; } if (SelectedPhotoSetID > 0) { DeletePhoto(SelectedPhotoSetID); BindPhotos(); } } protected void btnSubmit_Click(object sender, EventArgs e) { Page.Validate("Upload"); if (Page.IsValid) { if (PhotoSetID == 0) Database.PhotoSets_Add(SelectorID, FileSingle.Filename, txtCaption.Text); else Database.PhotoSets_Update(PhotoSetID, SelectorID, FileSingle.Filename, txtCaption.Text); PhotoSetID = 0; // all we have to do is clear the state litPhotoSetID.Text = "New Photo"; FileSingle.Filename = ""; txtCaption.Text = ""; BindPhotos(); } } protected void dlPhotos_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "ed") { PhotoSetID = (int)dlPhotos.DataKeys[e.Item.ItemIndex]; BindEdit(); } } private void DeletePhoto(int psId) { DataRow dr = Database.PhotoSets_Get(psId); string Filename = (string)dr["Filename"]; Utilities.DeleteProductPhoto(Filename); Database.PhotoSets_Delete(psId); } protected void custFileSingle_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = (FileSingle.Filename.Length > 0); } }