如何在回发上保持变量

Bri*_*ght 23 vb.net asp.net

我创建了一个单独的页面(代码在.vb后面)并创建了Public intFileID As 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
Run Code Online (Sandbox Code Playgroud)

"提交"按钮有一个单击事件,它根据intFileID变量的值插入或更新记录.我需要能够在回发上坚持这一价值,让一切顺利.

该页面只是在SQL数据库中插入或更新记录.我没有使用gridview,formview,detailsview或任何其他rad类型的对象,它自己持有键值,我不想使用它们中的任何一个.

如何在不在HTML中创建可能被更改的内容的情况下持久保存intFileID中设置的值.

[编辑]更改了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
Run Code Online (Sandbox Code Playgroud)

jer*_*ith 43

正如其他人指出的那样,您可以将它存储在Session或ViewState中.如果它是特定于页面的,我喜欢将它存储在ViewState而不是Session中,但我不知道一种方法是否优先于另一种方法.

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

ViewState(key) = value
Run Code Online (Sandbox Code Playgroud)

并检索它像:

value = ViewState(key)
Run Code Online (Sandbox Code Playgroud)


Kon*_*Kon 5

存储在:

  • 会议
  • 的ViewState
  • 隐藏的输入


Mit*_*ers 5

只是总结一下上面说的话.

您可以使用Session,Viewstate或隐藏字段.

我个人更喜欢viewstate,因为它可以在Web场环境中工作,Session不会,它不会将它存储在等待用户的服务器上,最多可以删除20分钟,而且一般来说viewstate是页面的位置水平数据.

您可以使用隐藏字段,但用户可以更轻松地修改它.