我创建了一个页面(代码位于.vb后面),并将Public intFileID创建为Integer

在页面加载中,我检查查询字符串并分配查询字符串(如果可用)或设置intFileID = 0。

Public intFileID As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        If Not Request.QueryString("fileid") Is Nothing Then
            intFileID = CInt(Request.QueryString("fileid"))
        End If

        If intFileID > 0 Then
            GetFile(intFileID)
        End If
    End If
End Sub

Private Sub GetFile()
    'uses intFileID to retrieve the specific record from database and set's the various textbox.text
End Sub

提交按钮存在单击事件,该事件基于intFileID变量的值插入或更新记录。我需要能够在回发时保持该值,以便所有这些正常工作。

该页面仅在SQL数据库中插入或更新记录。我没有使用gridview,formview,detailsview或任何其他rad类型对象,这些对象本身会保留键值,并且我不想使用它们中的任何一个。

我如何持久保存intFileID中设置的值而不在HTML中创建可能会更改的内容。

[编辑]更改了Page_Load以使用ViewState保留intFileID值
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        If Not Request.QueryString("fileid") Is Nothing Then
            intFileID = CInt(Request.QueryString("fileid"))
        End If

        If intFileID > 0 Then
            GetFile(intFileID)
        End If

        ViewState("intFileID") = intFileID
    Else
        intFileID = ViewState("intFileID")
    End If
End Sub

最佳答案

正如其他人指出的那样,您可以将其存储在Session或ViewState中。如果它是特定于页面的,我希望将其存储在ViewState中而不是Session中,但是我不知道一般来说,一种方法是否比另一种方法更可取。

在VB中,您可以将项目存储在ViewState中,例如:

ViewState(key) = value

并像这样检索它:
value = ViewState(key)

08-03 18:24