gpt4 book ai didi

asp.net - 如何在回发时保留变量

转载 作者:行者123 更新时间:2023-12-03 11:05:26 25 4
gpt4 key购买 nike

我创建了一个页面(带有 .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

提交按钮有一个单击事件,它根据 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)

关于asp.net - 如何在回发时保留变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/222999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com