在数据库中编辑记录时保留 ID

Preserving a ID when editing a record in Database

我试图在我的数据库中编辑一个团队,我获得了所有相关信息,但是当我单击 "OK" 时,所有变量都被重置,这是预期的行为,但我需要保留ID 以确保我提交编辑。我想知道是否有人知道如何做到这一点,我已经使用各种搜索词进行了谷歌搜索,结果都出现了我没有使用的 MVC。

相关代码如下。

Public Class 
Inherits System.Web.UI.Page

Private f_conAdministrator As OleDb.OleDbConnection ' Import System.Data.OleDB

' --------------------------------------------------------------------------------
' Form Variables
' --------------------------------------------------------------------------------
Private f_blnResult As Boolean      ' Don't use DialogResult since it triggers a cascade close
Private f_intTeamID As Integer

' --------------------------------------------------------------------------------
' Name: Page_Load
' Abstract: Handles the Page load event
' --------------------------------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Try

        Dim blnResult As Boolean = False

        ' First time the page is loaded?
        If Page.IsPostBack = False Then

            ' Load the exiting Team data
            blnResult = LoadTeam()

            ' Did it work?
            If blnResult = False Then

                ' No, warn the user ...
                SendMessageToClient("Unable to load team information from the database\n" & "The form will now close.")

                ' Close the form

                ' False = don't generate exception.  This is a planned redirect.
                Response.Redirect("./WManageTeams.aspx", False)

            End If

        End If

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

End Sub



' --------------------------------------------------------------------------------
' Name: LoadTeam
' Abstract: Get the team information from the database and populate the
'           form field with it
' --------------------------------------------------------------------------------
Private Function LoadTeam() As Boolean

    Dim blnResult As Boolean = False

    Try

        Dim udtTeam As udtTeamType = New udtTeamType

        ' Which team do we edit?
        f_intTeamID = Val(Request.QueryString("intTeamID"))
        udtTeam.intTeamID = f_intTeamID

        ' Open DB connection
        If OpenDatabaseConnectionMSAccess() = True Then

            ' Do it
            blnResult = GetTeamInformationFromDatabase(udtTeam)

            ' Did it work?
            If blnResult = True Then

                ' Yes
                txtTeam.Text = udtTeam.strTeam
                txtMascot.Text = udtTeam.strMascot

                ' Set focus to Team name and select existing text
                txtTeam.Focus()

            End If

            ' Close the conection
            CloseDatabaseConnection()

        End If

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

    Return blnResult

End Function



' --------------------------------------------------------------------------------
' Name: btnOK_Click
' Abstract: If the data is good then save the changes
' --------------------------------------------------------------------------------
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

    Try

        ' Is the data OK?
        If IsValidData() = True Then

            ' Open a connection
            If OpenDatabaseConnectionMSAccess() = True Then

                ' Yes, save
                If SaveData() = True Then

                    ' If the save was successful then ...

                    ' Success
                    f_blnResult = True

                End If

                ' Close the Connection
                CloseDatabaseConnection()

            End If

            ' Did it work?
            If f_blnResult = True Then

                ' Yes, redirect.  False = don't generate exception.  This is a planned redirect.
                Response.Redirect("./WManageTeams.aspx?intTeamID=" & f_intTeamID, False)

            End If

        End If

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

End Sub


' --------------------------------------------------------------------------------
' Name: GetTeamInformationFromDatabase
' Abstract: Get data for the specified team from the database
' --------------------------------------------------------------------------------
Public Function GetTeamInformationFromDatabase(ByRef udtTeam As udtTeamType) As Boolean

    Dim blnResult As Boolean = False

    Try

        Dim strSelect As String = ""
        Dim cmdSelect As New OleDb.OleDbCommand
        Dim drTTeams As OleDb.OleDbDataReader

        ' Build the select string
        strSelect = "SELECT *" & _
                    " FROM TTeams" & _
                    " WHERE intTeamID = " & udtTeam.intTeamID

        ' Retrieve the record
        cmdSelect = New OleDb.OleDbCommand(strSelect, f_conAdministrator)
        drTTeams = cmdSelect.ExecuteReader

        ' Read (there should be 1 and only 1 row)
        drTTeams.Read()
        With drTTeams
            udtTeam.strTeam = .Item("strTeam")
            udtTeam.strMascot = .Item("strMascot")
        End With

        ' Clean up
        drTTeams.Close()

        ' Success
        blnResult = True

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

    Return blnResult

End Function

和加载数据时第一次请求一样,也可以在PostBack请求中从请求参数中读取ID:

f_intTeamID = Val(Request.QueryString("intTeamID"))

为请求提供服务的实例只为特定的请求创建,然后被丢弃。当用户单击 OK 按钮后新请求到达服务器时,将创建一个新实例。这就是变量值消失的原因。

由于您可能在每个请求中都需要 ID,因此您可以将语句添加到 Page_Load 方法中,以便在需要时设置变量。

您可以将其保存在会话变量中,如下所示:-

f_intTeamID = Val(Request.QueryString("intTeamID"))
Session("f_intTeamID") = f_intTeamID 

下次当页面 post 返回时从会话中检索它。