Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I created a single page (with code behind .vb) and created Public intFileID As Integer

in the Page load I check for the querystring and assign it if available or set 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

There is a click event for the Submit button that inserts or updates a record based on the value of the intFileID variable. I need to be able to persist that value on postback for it all to work.

The page simply inserts or updates a record in a SQL database. I'm not using a gridview,formview,detailsview, or any other rad type object which persists the key value by itself and I don't want to use any of them.

How can I persist the value set in intFileID without creating something in the HTML which could possibly be changed.

[EDIT] Changed Page_Load to use ViewState to persist the intFileID value

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
575 views
Welcome To Ask or Share your Answers For Others

1 Answer

As others have pointed out, you can store it in the Session or the ViewState. If it's page specific, I like to store it in the ViewState as opposed to the Session, but I don't know if one method is generally preferred over the other.

In VB, you would store an item in the ViewState like:

ViewState(key) = value

And retrieve it like:

value = ViewState(key)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...