Reputation: 5
I'm trying to store a value in code behind so I don't have to clog my page with HiddenFields, but the value 'disappears'
I'm assuming it has to do with either scope or byval vs byref, but I don't know how I need to do it for it to work.
What I've tried to do is creating a Dim under my Partial Class, setting the value in a gridview.RowCommand, and then trying to get the value at a later button.Click
Partial Class CustNSSF_MinSide
Inherits System.Web.UI.Page
Dim vr As New vrClass
Dim _ActNo As String
Sub GV_Relations_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GV_Relations.RowCommand
_ActNo = GV_Relations.DataKeys(index)("ActSeqNo")
Protected Sub btn_lagre_Click(sender As Object, e As System.EventArgs) Handles btn_lagre.Click
Dim test = _ActNo
Upvotes: 0
Views: 131
Reputation: 460068
The value disappears because all variables (or controls) are disposed at the end of every page's life-cycle. GV_Relations_RowCommand
is triggered only on RowCommand
and btn_lagre_Click
is a different action. You could store this value in a Session
variable, ViewState
or (as you've done) in a HiddenField
. So when the user clicked on btn_lagre
, the previous action that caused RowCommand
was a different postback, therefore the variable is Nothing
.
So one way (apart from your HiddenField
aproach), using ViewState
:
Private Property ActSeqNo As System.Int32
Get
If ViewState("ActSeqNo") Is Nothing Then
ViewState("ActSeqNo") = System.Int32.MinValue
End If
Return DirectCast(ViewState("ActSeqNo"), System.Int32)
End Get
Set(value As System.Int32)
ViewState("ActSeqNo") = value
End Set
End Property
Then you can set it in this way:
Me.ActSeqNo = System.Int32.Parse(GV_Relations.DataKeys(index)("ActSeqNo")))
Nine Options for Managing Persistent User State in ASP.NET
Upvotes: 1
Reputation: 31239
The variable will be clear every Postback
. You need to store it either in the Viewstate
or in the Session
. You could do this:
Property _ActNo As String
Get
Return ViewState("_ActNo")
End Get
Set(ByVal value As String)
ViewState("_ActNo") = value
End Set
End Property
Upvotes: 0