%@ CodePage=65001 %> <% Const EW_PAGE_ID = "add" Const EW_TABLE_NAME = "Photo_Gallery" %> <% Session.Timeout = 100 %> <% Response.Expires = 0 Response.ExpiresAbsolute = Now() - 1 Response.AddHeader "pragma", "no-cache" Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate" %> <% ' Open connection to the database Dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.Open EW_DB_CONNECTION_STRING %> <% Dim Security Set Security = New cAdvancedSecurity %> <% If Not Security.IsLoggedIn() Then Call Security.AutoLogin() If Not Security.IsLoggedIn() Then Call Security.SaveLastUrl() Call Page_Terminate("login.asp") End If %> <% ' Common page loading event (in userfn60.asp) Call Page_Loading() %> <% ' Page load event, used in current page Call Page_Load() %> <% Response.Buffer = True ' Load key values from QueryString Dim bCopy bCopy = True If Request.QueryString("ID").Count > 0 Then Photo_Gallery.ID.QueryStringValue = Request.QueryString("ID") Else bCopy = False End If ' Create form object Dim objForm Set objForm = ew_GetUploadObj() ' Process form if post back If objForm.GetValue("a_add")&"" <> "" Then Photo_Gallery.CurrentAction = objForm.GetValue("a_add") ' Get form action Call GetUploadFiles() ' Get upload files Call LoadFormValues() ' Load form values ' Not post back Else If bCopy Then Photo_Gallery.CurrentAction = "C" ' Copy Record Else Photo_Gallery.CurrentAction = "I" ' Display Blank Record Call LoadDefaultValues() ' Load default values End If End If ' Close form object Set objForm = Nothing ' Perform action based on action code Select Case Photo_Gallery.CurrentAction Case "I" ' Blank record, no action required Case "C" ' Copy an existing record If Not LoadRow() Then ' Load record based on key Session(EW_SESSION_MESSAGE) = "No records found" ' No record found Call Page_Terminate(Photo_Gallery.ReturnUrl) ' Clean up and return End If Case "A" ' Add new record Photo_Gallery.SendEmail = True ' Send email on add success If AddRow() Then ' Add successful Session(EW_SESSION_MESSAGE) = "Add New Record Successful" ' Set up success message Call Page_Terminate(Photo_Gallery.KeyUrl(Photo_Gallery.ReturnUrl, "")) ' Clean up and return Else Call RestoreFormValues() ' Add failed, restore form values End If End Select ' Render row based on row type Photo_Gallery.RowType = EW_ROWTYPE_ADD ' add ' Render add type Call RenderRow() %>
Add to TABLE: Photo Gallery
Go Back
<%= Session(EW_SESSION_MESSAGE) %>
<% Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session End If %> <% ' If control is passed here, simply terminate the page without redirect Call Page_Terminate("") ' ----------------------------------------------------------------- ' Subroutine Page_Terminate ' - called when exit page ' - clean up ADO connection and objects ' - if url specified, redirect to url, otherwise end response ' Sub Page_Terminate(url) ' Page unload event, used in current page Call Page_Unload() ' Global page unloaded event (in userfn60.asp) Call Page_Unloaded() conn.Close ' Close Connection Set conn = Nothing Set Security = Nothing Set Photo_Gallery = Nothing ' Go to url if specified If url <> "" Then Response.Clear Response.Redirect url End If ' Terminate response Response.End End Sub ' ' Subroutine Page_Terminate (End) ' ---------------------------------------- %> <% ' Function Get upload files Function GetUploadFiles() ' Get upload data If Photo_Gallery.Thumb_File.Upload.UploadFile() Then ' No action required Else Response.Write Photo_Gallery.Thumb_File.Upload.Message Response.End End If If Photo_Gallery.Photo_File.Upload.UploadFile() Then ' No action required Else Response.Write Photo_Gallery.Photo_File.Upload.Message Response.End End If End Function %> <% ' Load default values Function LoadDefaultValues() End Function %> <% ' Load form values Function LoadFormValues() ' Load from form Photo_Gallery.Photo_Category.FormValue = objForm.GetValue("x_Photo_Category") Photo_Gallery.Photo_Title.FormValue = objForm.GetValue("x_Photo_Title") End Function ' Restore form values Function RestoreFormValues() Photo_Gallery.Photo_Category.CurrentValue = Photo_Gallery.Photo_Category.FormValue Photo_Gallery.Photo_Title.CurrentValue = Photo_Gallery.Photo_Title.FormValue End Function %> <% ' Load row based on key values Function LoadRow() Dim rs, sSql, sFilter sFilter = Photo_Gallery.SqlKeyFilter If Not IsNumeric(Photo_Gallery.ID.CurrentValue) Then LoadRow = False ' Invalid key, exit Exit Function End If sFilter = Replace(sFilter, "@ID@", ew_AdjustSql(Photo_Gallery.ID.CurrentValue)) ' Replace key value ' Call Row Selecting event Call Photo_Gallery.Row_Selecting(sFilter) ' Load sql based on filter Photo_Gallery.CurrentFilter = sFilter sSql = Photo_Gallery.SQL Set rs = Server.CreateObject("ADODB.Recordset") rs.Open sSql, conn If rs.Eof Then LoadRow = False Else LoadRow = True rs.MoveFirst Call LoadRowValues(rs) ' Load row values ' Call Row Selected event Call Photo_Gallery.Row_Selected(rs) End If rs.Close Set rs = Nothing End Function ' Load row values from recordset Sub LoadRowValues(rs) Photo_Gallery.ID.DbValue = rs("ID") Photo_Gallery.Photo_Category.DbValue = rs("Photo_Category") Photo_Gallery.Photo_Title.DbValue = rs("Photo_Title") Photo_Gallery.Thumb_File.Upload.DbValue = rs("Thumb_File") Photo_Gallery.Photo_File.Upload.DbValue = rs("Photo_File") Photo_Gallery.CreatedDate.DbValue = rs("CreatedDate") End Sub %> <% ' Render row values based on field settings Sub RenderRow() ' Call Row Rendering event Call Photo_Gallery.Row_Rendering() ' Common render codes for all row types ' Photo_Category Photo_Gallery.Photo_Category.CellCssStyle = "" Photo_Gallery.Photo_Category.CellCssClass = "" ' Photo_Title Photo_Gallery.Photo_Title.CellCssStyle = "" Photo_Gallery.Photo_Title.CellCssClass = "" ' Thumb_File Photo_Gallery.Thumb_File.CellCssStyle = "" Photo_Gallery.Thumb_File.CellCssClass = "" ' Photo_File Photo_Gallery.Photo_File.CellCssStyle = "" Photo_Gallery.Photo_File.CellCssClass = "" If Photo_Gallery.RowType = EW_ROWTYPE_VIEW Then ' View row ElseIf Photo_Gallery.RowType = EW_ROWTYPE_ADD Then ' Add row ' Photo_Category Photo_Gallery.Photo_Category.EditCustomAttributes = "" sSqlWrk = "SELECT [ID], [Category_Name] FROM [Gallery_Category]" sSqlWrk = sSqlWrk & " ORDER BY [Category_Name] Asc" Set rswrk = Server.CreateObject("ADODB.Recordset") rswrk.Open sSqlWrk, conn If Not rswrk.Eof Then arwrk = rswrk.GetRows Else arwrk = "" End If rswrk.Close Set rswrk = Nothing arwrk = ew_AddItemToArray(arwrk, 0, Array("", "Please Select")) Photo_Gallery.Photo_Category.EditValue = arwrk ' Photo_Title Photo_Gallery.Photo_Title.EditCustomAttributes = "" Photo_Gallery.Photo_Title.EditValue = ew_HtmlEncode(Photo_Gallery.Photo_Title.CurrentValue) ' Thumb_File Photo_Gallery.Thumb_File.EditCustomAttributes = "" If Not IsNull(Photo_Gallery.Thumb_File.Upload.DbValue) Then Photo_Gallery.Thumb_File.EditValue = Photo_Gallery.Thumb_File.Upload.DbValue Photo_Gallery.Thumb_File.ImageAlt = "" Else Photo_Gallery.Thumb_File.EditValue = "" End If ' Photo_File Photo_Gallery.Photo_File.EditCustomAttributes = "" If Not IsNull(Photo_Gallery.Photo_File.Upload.DbValue) Then Photo_Gallery.Photo_File.EditValue = Photo_Gallery.Photo_File.Upload.DbValue Photo_Gallery.Photo_File.ImageAlt = "" Else Photo_Gallery.Photo_File.EditValue = "" End If ElseIf Photo_Gallery.RowType = EW_ROWTYPE_EDIT Then ' Edit row ElseIf Photo_Gallery.RowType = EW_ROWTYPE_SEARCH Then ' Search row End If ' Call Row Rendered event Call Photo_Gallery.Row_Rendered() End Sub %> <% ' Add record Function AddRow() On Error Resume Next Dim rs, sSql, sFilter Dim rsnew Dim bCheckKey, sSqlChk, sWhereChk, rsChk Dim bInsertRow ' Check for duplicate key bCheckKey = True sFilter = Photo_Gallery.SqlKeyFilter If Photo_Gallery.ID.CurrentValue = "" Or IsNull(Photo_Gallery.ID.CurrentValue) Then bCheckKey = False Else sFilter = Replace(sFilter, "@ID@", ew_AdjustSql(Photo_Gallery.ID.CurrentValue)) ' Replace key value End If If Not IsNumeric(Photo_Gallery.ID.CurrentValue) Then bCheckKey = False End If If bCheckKey Then Set rsChk = Photo_Gallery.LoadRs(sFilter) If Not (rsChk Is Nothing) Then Session(EW_SESSION_MESSAGE) = "Duplicate value for primary key" rsChk.Close Set rsChk = Nothing AddRow = False Exit Function End If End If ' Add new record sFilter = "(0 = 1)" Photo_Gallery.CurrentFilter = sFilter sSql = Photo_Gallery.SQL Set rs = Server.CreateObject("ADODB.Recordset") rs.CursorLocation = EW_CURSORLOCATION rs.Open sSql, conn, 1, 2 rs.AddNew If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description rs.Close Set rs = Nothing AddRow = False Exit Function End If ' Field Photo_Category Call Photo_Gallery.Photo_Category.SetDbValue(Photo_Gallery.Photo_Category.CurrentValue, Null) rs("Photo_Category") = Photo_Gallery.Photo_Category.DbValue ' Field Photo_Title Call Photo_Gallery.Photo_Title.SetDbValue(Photo_Gallery.Photo_Title.CurrentValue, Null) rs("Photo_Title") = Photo_Gallery.Photo_Title.DbValue ' Field Thumb_File Call Photo_Gallery.Thumb_File.Upload.SaveToSession() ' Save file value to Session Photo_Gallery.Thumb_File.Upload.DbValue = rs("Thumb_File") ' Get original value If IsNull(Photo_Gallery.Thumb_File.Upload.Value) Then rs("Thumb_File") = Null Else rs("Thumb_File") = ew_UploadFileNameEx(ew_UploadPathEx(True, "photos/"), Photo_Gallery.Thumb_File.Upload.FileName) End If ' Field Photo_File Call Photo_Gallery.Photo_File.Upload.SaveToSession() ' Save file value to Session Photo_Gallery.Photo_File.Upload.DbValue = rs("Photo_File") ' Get original value If IsNull(Photo_Gallery.Photo_File.Upload.Value) Then rs("Photo_File") = Null Else rs("Photo_File") = ew_UploadFileNameEx(ew_UploadPathEx(True, "photos/"), Photo_Gallery.Photo_File.Upload.FileName) End If ' Check recordset update error If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description rs.Close Set rs = Nothing AddRow = False Exit Function End If ' Call Row Inserting event bInsertRow = Photo_Gallery.Row_Inserting(rs) If bInsertRow Then ' Field Thumb_File If Not IsNull(Photo_Gallery.Thumb_File.Upload.Value) Then Call ew_SaveFile(ew_UploadPathEx(True, "photos/"), rs("Thumb_File"), Photo_Gallery.Thumb_File.Upload.Value) If Photo_Gallery.Thumb_File.Upload.Action = "2" Or Photo_Gallery.Thumb_File.Upload.Action = "3" Then ' Update/Remove If Photo_Gallery.Thumb_File.Upload.DbValue <> "" Then ew_DeleteFile ew_UploadPathEx(True, "photos/") & Photo_Gallery.Thumb_File.Upload.DbValue End If ' Field Photo_File If Not IsNull(Photo_Gallery.Photo_File.Upload.Value) Then Call ew_SaveFile(ew_UploadPathEx(True, "photos/"), rs("Photo_File"), Photo_Gallery.Photo_File.Upload.Value) If Photo_Gallery.Photo_File.Upload.Action = "2" Or Photo_Gallery.Photo_File.Upload.Action = "3" Then ' Update/Remove If Photo_Gallery.Photo_File.Upload.DbValue <> "" Then ew_DeleteFile ew_UploadPathEx(True, "photos/") & Photo_Gallery.Photo_File.Upload.DbValue End If ' Clone new rs object Set rsnew = ew_CloneRs(rs) rs.Update If Err.Number <> 0 Then Session(EW_SESSION_MESSAGE) = Err.Description AddRow = False Else AddRow = True End If Else rs.CancelUpdate If Photo_Gallery.CancelMessage <> "" Then Session(EW_SESSION_MESSAGE) = Photo_Gallery.CancelMessage Photo_Gallery.CancelMessage = "" Else Session(EW_SESSION_MESSAGE) = "Insert cancelled" End If AddRow = False End If rs.Close Set rs = Nothing If AddRow Then Photo_Gallery.ID.DbValue = rsnew("ID") ' Call Row Inserted event Call Photo_Gallery.Row_Inserted(rsnew) End If ' Field Thumb_File Call Photo_Gallery.Thumb_File.Upload.RemoveFromSession() ' Remove file value from Session ' Field Photo_File Call Photo_Gallery.Photo_File.Upload.RemoveFromSession() ' Remove file value from Session If IsObject(rsnew) Then rsnew.Close Set rsnew = Nothing End If End Function %> <% ' Page Load event Sub Page_Load() '***Response.Write "Page Load" Server.ScriptTimeout=1000 End Sub ' Page Unload event Sub Page_Unload() '***Response.Write "Page Unload" End Sub %>