home *** CD-ROM | disk | FTP | other *** search
/ 66.142.0.142 / 66.142.0.142.tar / 66.142.0.142 / tblPartsedit.aspx.vb < prev    next >
Text File  |  2010-08-24  |  13KB  |  508 lines

  1. Imports System.Data
  2. Imports System.Data.Common
  3. Imports System.Xml
  4. Imports System.IO
  5. Imports System.Data.OleDb
  6.  
  7. '
  8. ' ASP.NET code-behind class (Page)
  9. '
  10.  
  11. Partial Class tblPartsedit
  12.     Inherits AspNetMaker7_tfpssnet
  13.  
  14.     ' Page object
  15.     Public tblParts_edit As ctblParts_edit
  16.  
  17.     '
  18.     ' Page Class
  19.     '
  20.     Class ctblParts_edit
  21.         Inherits AspNetMakerPage
  22.         Implements IDisposable        
  23.  
  24.         ' Used by system generated functions
  25.         Private RsWrk As Object, sSqlWrk As String, sWhereWrk As String
  26.  
  27.         Private arwrk As Object
  28.  
  29.         Private armultiwrk() As String        
  30.  
  31.         ' Page URL
  32.         Public ReadOnly Property PageUrl() As String
  33.             Get
  34.                 Dim Url As String = ew_CurrentPage() & "?"
  35.                 If tblParts.UseTokenInUrl Then Url = Url & "t=" & tblParts.TableVar & "&" ' Add page token
  36.                 Return Url    
  37.             End Get
  38.         End Property
  39.  
  40.         ' Validate page request
  41.         Public Function IsPageRequest() As Boolean
  42.             Dim Result As Boolean
  43.             If tblParts.UseTokenInUrl Then
  44.                 Result = False
  45.                 If ObjForm IsNot Nothing Then
  46.                     Result = (tblParts.TableVar = ObjForm.GetValue("t"))
  47.                 End If
  48.                 If ew_Get("t") <> "" Then
  49.                     Result = (tblParts.TableVar = ew_Get("t"))
  50.                 End If
  51.                 Return Result
  52.             End If
  53.             Return True            
  54.         End Function    
  55.  
  56.         ' tblParts
  57.         Public Property tblParts() As ctblParts
  58.             Get                
  59.                 Return ParentPage.tblParts
  60.             End Get
  61.             Set(ByVal v As ctblParts)
  62.                 ParentPage.tblParts = v    
  63.             End Set    
  64.         End Property
  65.  
  66.         ' tblParts
  67.         Public Property tblEmployees() As ctblEmployees
  68.             Get                
  69.                 Return ParentPage.tblEmployees
  70.             End Get
  71.             Set(ByVal v As ctblEmployees)
  72.                 ParentPage.tblEmployees = v    
  73.             End Set    
  74.         End Property
  75.  
  76.         '
  77.         '  Constructor
  78.         '  - init objects
  79.         '  - open connection
  80.         '
  81.         Public Sub New(ByRef APage As AspNetMaker7_tfpssnet)                
  82.             m_ParentPage = APage
  83.             m_Page = Me    
  84.             m_PageID = "edit"
  85.             m_PageObjName = "tblParts_edit"
  86.             m_PageObjTypeName = "ctblParts_edit"
  87.  
  88.             ' Table Name
  89.             m_TableName = "tblParts"
  90.  
  91.             ' Initialize table object
  92.             tblParts = New ctblParts(Me)
  93.             tblEmployees = New ctblEmployees(Me)
  94.  
  95.             ' Connect to database
  96.             Conn = New cConnection()
  97.         End Sub
  98.  
  99.         '
  100.         '  Subroutine Page_Init
  101.         '  - called before page main
  102.         '  - check Security
  103.         '  - set up response header
  104.         '  - call page load events
  105.         '
  106.         Public Sub Page_Init()
  107.             Security = New cAdvancedSecurity(Me)
  108.             If Not Security.IsLoggedIn() Then Security.AutoLogin()
  109.             If Not Security.IsLoggedIn() Then
  110.                 Security.SaveLastUrl()
  111.                 Page_Terminate("login.aspx")
  112.             End If
  113.  
  114.             ' Table Permission loading event
  115.             Security.TablePermission_Loading()
  116.             Security.LoadCurrentUserLevel(TableName)
  117.  
  118.             ' Table Permission loaded event
  119.             Security.TablePermission_Loaded()
  120.             If Not Security.CanEdit Then
  121.                 Security.SaveLastUrl()
  122.                 Page_Terminate("tblPartslist.aspx")
  123.             End If
  124.  
  125.             ' User ID loading event
  126.             Security.UserID_Loading()
  127.             If Security.IsLoggedIn() Then Call Security.LoadUserID()
  128.  
  129.             ' User ID loaded event
  130.             Security.UserID_Loaded()
  131.  
  132.             ' Global page loading event (in ewglobal*.vb)
  133.             ParentPage.Page_Loading()
  134.  
  135.             ' Page load event, used in current page
  136.             Page_Load()
  137.         End Sub
  138.  
  139.         '
  140.         '  Class terminate
  141.         '  - clean up page object
  142.         '
  143.         Public Sub Dispose() Implements IDisposable.Dispose
  144.             Page_Terminate("")
  145.         End Sub
  146.  
  147.         '
  148.         '  Sub Page_Terminate
  149.         '  - called when exit page
  150.         '  - clean up connection and objects
  151.         '  - if URL specified, redirect to URL
  152.         '
  153.         Sub Page_Terminate(url As String)
  154.  
  155.             ' Page unload event, used in current page
  156.             Page_Unload()
  157.  
  158.             ' Global page unloaded event (in ewglobal*.vb)
  159.             ParentPage.Page_Unloaded()
  160.  
  161.             ' Close connection
  162.             Conn.Dispose()
  163.             Security = Nothing
  164.             tblParts.Dispose()
  165.             tblEmployees.Dispose()
  166.  
  167.             ' Go to URL if specified
  168.             If url <> "" Then
  169.                 HttpContext.Current.Response.Clear()
  170.                 HttpContext.Current.Response.Redirect(url)
  171.             End If
  172.         End Sub
  173.  
  174.     '
  175.     ' Page main processing
  176.     '
  177.     Sub Page_Main()
  178.  
  179.         ' Load key from QueryString
  180.         If ew_Get("prtID") <> "" Then
  181.             tblParts.prtID.QueryStringValue = ew_Get("prtID")
  182.         End If
  183.  
  184.         ' Create form object
  185.         ObjForm = New cFormObj
  186.         If ObjForm.GetValue("a_edit") <> "" Then
  187.             tblParts.CurrentAction = ObjForm.GetValue("a_edit") ' Get action code
  188.             LoadFormValues() ' Get form values
  189.  
  190.             ' Validate Form
  191.             If Not ValidateForm() Then
  192.                 tblParts.CurrentAction = "" ' Form error, reset action
  193.                 Message = ParentPage.gsFormError
  194.                 LoadRow() ' Restore row
  195.                 RestoreFormValues() ' Restore form values if validate failed
  196.             End If
  197.         Else
  198.             tblParts.CurrentAction = "I" ' Default action is display
  199.         End If
  200.  
  201.         ' Check if valid key
  202.         If ew_Empty(tblParts.prtID.CurrentValue) Then Page_Terminate("tblPartslist.aspx") ' Invalid key, return to list
  203.         Select Case tblParts.CurrentAction
  204.             Case "I" ' Get a record to display
  205.                 If Not LoadRow() Then ' Load Record based on key
  206.                     Message = "No records found" ' No record found
  207.                     Page_Terminate("tblPartslist.aspx") ' No matching record, return to list
  208.                 End If
  209.             Case "U" ' Update
  210.                 tblParts.SendEmail = True ' Send email on update success
  211.                 If EditRow() Then ' Update Record based on key
  212.                     Message = "Update succeeded" ' Update success
  213.                     Dim sReturnUrl As String = tblParts.ReturnUrl
  214.                     Page_Terminate(sReturnUrl) ' Return to caller
  215.                 Else
  216.                     LoadRow() ' Restore row
  217.                     RestoreFormValues() ' Restore form values if update failed
  218.                 End If
  219.         End Select
  220.  
  221.         ' Render the record
  222.         tblParts.RowType = EW_ROWTYPE_EDIT ' Render as edit
  223.  
  224.         ' Render row
  225.         RenderRow()
  226.     End Sub
  227.  
  228.     '
  229.     ' Get upload file
  230.     '
  231.     Sub GetUploadFiles()
  232.  
  233.         ' Get upload data
  234.     End Sub
  235.  
  236.     '
  237.     ' Load default values
  238.     '
  239.     Sub LoadDefaultValues()
  240.     End Sub
  241.  
  242.     '
  243.     ' Load form values
  244.     '
  245.     Sub LoadFormValues()
  246.         tblParts.prtDescription.FormValue = ObjForm.GetValue("x_prtDescription")
  247.         tblParts.prtDescription.OldValue = ObjForm.GetValue("o_prtDescription")
  248.         tblParts.prtID.FormValue = ObjForm.GetValue("x_prtID")
  249.     End Sub
  250.  
  251.     '
  252.     ' Restore form values
  253.     '
  254.     Sub RestoreFormValues()
  255.         tblParts.prtDescription.CurrentValue = tblParts.prtDescription.FormValue
  256.         tblParts.prtID.CurrentValue = tblParts.prtID.FormValue
  257.     End Sub
  258.  
  259.     '
  260.     ' Load row based on key values
  261.     '
  262.     Function LoadRow() As Boolean
  263.         Dim RsRow As OleDbDataReader
  264.         Dim sFilter As String = tblParts.KeyFilter
  265.  
  266.         ' Row Selecting event
  267.         tblParts.Row_Selecting(sFilter)
  268.  
  269.         ' Load SQL based on filter
  270.         tblParts.CurrentFilter = sFilter
  271.         Dim sSql As String = tblParts.SQL
  272.  
  273.         ' Write SQL for debug
  274.         If EW_DEBUG_ENABLED Then ew_Write(sSql)
  275.         Try
  276.             RsRow = Conn.GetTempDataReader(sSql)    
  277.             If Not RsRow.Read() Then
  278.                 Return False
  279.             Else                
  280.                 LoadRowValues(RsRow) ' Load row values
  281.  
  282.                 ' Row Selected event
  283.                 tblParts.Row_Selected(RsRow)
  284.                 Return True    
  285.             End If
  286.         Catch
  287.             If EW_DEBUG_ENABLED Then Throw
  288.             Return False
  289.         Finally
  290.             Conn.CloseTempDataReader()
  291.         End Try
  292.     End Function
  293.  
  294.     '
  295.     ' Load row values from recordset
  296.     '
  297.     Sub LoadRowValues(ByRef RsRow As OleDbDataReader)
  298.         tblParts.prtID.DbValue = RsRow("prtID")
  299.         tblParts.prtDescription.DbValue = RsRow("prtDescription")
  300.     End Sub
  301.  
  302.     '
  303.     ' Render row values based on field settings
  304.     '
  305.     Sub RenderRow()
  306.  
  307.         ' Row Rendering event
  308.         tblParts.Row_Rendering()
  309.  
  310.         '
  311.         '  Common render codes for all row types
  312.         '
  313.         ' prtDescription
  314.  
  315.         tblParts.prtDescription.CellCssStyle = ""
  316.         tblParts.prtDescription.CellCssClass = ""
  317.  
  318.         '
  319.         '  View  Row
  320.         '
  321.  
  322.         If tblParts.RowType = EW_ROWTYPE_VIEW Then ' View row
  323.  
  324.             ' prtDescription
  325.             tblParts.prtDescription.ViewValue = tblParts.prtDescription.CurrentValue
  326.             tblParts.prtDescription.CssStyle = ""
  327.             tblParts.prtDescription.CssClass = ""
  328.             tblParts.prtDescription.ViewCustomAttributes = ""
  329.  
  330.             ' View refer script
  331.             ' prtDescription
  332.  
  333.             tblParts.prtDescription.HrefValue = ""
  334.  
  335.         '
  336.         '  Edit Row
  337.         '
  338.  
  339.         ElseIf tblParts.RowType = EW_ROWTYPE_EDIT Then ' Edit row
  340.  
  341.             ' prtDescription
  342.             tblParts.prtDescription.EditCustomAttributes = ""
  343.             tblParts.prtDescription.EditValue = ew_HtmlEncode(tblParts.prtDescription.CurrentValue)
  344.  
  345.             ' Edit refer script
  346.             ' prtDescription
  347.  
  348.             tblParts.prtDescription.HrefValue = ""
  349.         End If
  350.  
  351.         ' Row Rendered event
  352.         tblParts.Row_Rendered()
  353.     End Sub
  354.  
  355.     '
  356.     ' Validate form
  357.     '
  358.     Function ValidateForm() As Boolean
  359.  
  360.         ' Initialize
  361.         ParentPage.gsFormError = ""
  362.  
  363.         ' Check if validation required
  364.         If Not EW_SERVER_VALIDATE Then Return (ParentPage.gsFormError = "")
  365.  
  366.         ' Return validate result
  367.         Dim Valid As Boolean = (ParentPage.gsFormError = "")
  368.  
  369.         ' Form_CustomValidate event
  370.         Dim sFormCustomError As String = ""
  371.         Valid = Valid And Form_CustomValidate(sFormCustomError)
  372.         If sFormCustomError <> "" Then
  373.             If ParentPage.gsFormError <> "" Then ParentPage.gsFormError = ParentPage.gsFormError & "<br />"
  374.             ParentPage.gsFormError = ParentPage.gsFormError & sFormCustomError
  375.         End If
  376.         Return Valid
  377.     End Function
  378.  
  379.     '
  380.     ' Update record based on key values
  381.     '
  382.     Function EditRow() As Boolean
  383.         Dim RsEdit As OleDbDataReader, RsChk As OleDbDataReader
  384.         Dim sSql As String, sFilter As String
  385.         Dim sSqlChk As String, sFilterChk As String
  386.         Dim bUpdateRow As Boolean
  387.         Dim RsOld As OrderedDictionary
  388.         Dim sIdxErrMsg As String
  389.         Dim Rs As New OrderedDictionary
  390.         sFilter = tblParts.KeyFilter
  391.         If tblParts.prtDescription.CurrentValue <> "" Then ' Check field with unique index
  392.             sFilterChk = "(prtDescription = '" & ew_AdjustSql(tblParts.prtDescription.CurrentValue) & "')"
  393.             sFilterChk = sFilterChk & " AND NOT (" & sFilter & ")"
  394.             tblParts.CurrentFilter = sFilterChk
  395.             sSqlChk = tblParts.SQL
  396.             Try
  397.                 RsChk = Conn.GetDataReader(sSqlChk)
  398.                 If RsChk.Read() Then
  399.                     sIdxErrMsg = "Duplicate value '%v' for unique index '%f'".Replace("%f", "prtDescription")
  400.                     sIdxErrMsg = sIdxErrMsg.Replace("%v", tblParts.prtDescription.CurrentValue)
  401.                     Message = sIdxErrMsg            
  402.                     Return False
  403.                 End If
  404.             Catch e As Exception
  405.                 If EW_DEBUG_ENABLED Then Throw
  406.                 Message = e.Message
  407.                 Return False
  408.             Finally
  409.                 RsChk.Close()
  410.                 RsChk.Dispose()    
  411.             End Try                
  412.         End If
  413.         tblParts.CurrentFilter  = sFilter
  414.         sSql = tblParts.SQL
  415.         Try
  416.             RsEdit = Conn.GetDataReader(sSql) 
  417.         Catch e As Exception
  418.             If EW_DEBUG_ENABLED Then Throw
  419.             Message = e.Message
  420.             RsEdit.Close()
  421.             EditRow = False
  422.         End Try
  423.         If Not RsEdit.Read() Then
  424.             RsEdit.Close()
  425.             EditRow = False ' Update Failed
  426.         Else
  427.             RsOld = Conn.GetRow(RsEdit)
  428.             RsEdit.Close()
  429.  
  430.             ' prtDescription
  431.             tblParts.prtDescription.SetDbValue(tblParts.prtDescription.CurrentValue, System.DBNull.Value)
  432.             Rs("prtDescription") = tblParts.prtDescription.DbValue
  433.  
  434.             ' Row Updating event
  435.             bUpdateRow = tblParts.Row_Updating(RsOld, Rs)
  436.             If bUpdateRow Then
  437.                 Try
  438.                     tblParts.Update(Rs)
  439.                     EditRow = True
  440.                 Catch e As Exception
  441.                     If EW_DEBUG_ENABLED Then Throw
  442.                     Message = e.Message
  443.                     EditRow = False
  444.                 End Try
  445.             Else
  446.                 If tblParts.CancelMessage <> "" Then
  447.                     Message = tblParts.CancelMessage
  448.                     tblParts.CancelMessage = ""
  449.                 Else
  450.                     Message = "Update cancelled"
  451.                 End If
  452.                 EditRow = False
  453.             End If
  454.         End If
  455.  
  456.         ' Row Updated event
  457.         If EditRow Then
  458.             tblParts.Row_Updated(RsOld, Rs)
  459.         End If
  460.     End Function
  461.  
  462.         ' Page Load event
  463.         Public Sub Page_Load()
  464.  
  465.             'HttpContext.Current.Response.Write("Page Load")
  466.         End Sub
  467.  
  468.         ' Page Unload event
  469.         Public Sub Page_Unload()
  470.  
  471.             'HttpContext.Current.Response.Write("Page Unload")
  472.         End Sub
  473.  
  474.     ' Form Custom Validate event
  475.     Public Function Form_CustomValidate(ByRef CustomError As String) As Boolean
  476.  
  477.         'Return error message in CustomError
  478.         Return True
  479.     End Function
  480.     End Class
  481.  
  482.     '
  483.     ' ASP.NET Page_Load event
  484.     '
  485.  
  486.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  487.         Response.Buffer = EW_RESPONSE_BUFFER
  488.         Response.Cache.SetCacheability(HttpCacheability.NoCache)
  489.  
  490.         ' Page init
  491.         tblParts_edit = New ctblParts_edit(Me)        
  492.         tblParts_edit.Page_Init()
  493.  
  494.         ' Page main processing
  495.         tblParts_edit.Page_Main()
  496.     End Sub
  497.  
  498.     '
  499.     ' ASP.NET Page_Unload event
  500.     '
  501.  
  502.     Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
  503.  
  504.         ' Dispose page object
  505.         If tblParts_edit IsNot Nothing Then tblParts_edit.Dispose()
  506.     End Sub
  507. End Class
  508.