加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在回发上保持变量

发布时间:2020-12-16 00:47:33 所属栏目:asp.Net 来源:网络整理
导读:我创建了一个单独的页面(代码为.vb),并创建了Public intFileID As Integer 在页面加载中,我检查查询字符串并将其分配(如果可用)或设置intFileID = 0。 Public intFileID As Integer = 0Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.
我创建了一个单独的页面(代码为.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,detailview或任何其他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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读