vb.net中的Dim值
我试图在代码隐藏中存储一个值,这样我就不必用HiddenFields阻塞我的页面,但该值“消失” 我假设它与scope或byval vs byref有关,但我不知道如何才能工作 我试图做的是在我的分部类下创建一个Dim,在gridview.row命令中设置值,然后在稍后的按钮上尝试获取值。单击vb.net中的Dim值,vb.net,code-behind,Vb.net,Code Behind,我试图在代码隐藏中存储一个值,这样我就不必用HiddenFields阻塞我的页面,但该值“消失” 我假设它与scope或byval vs byref有关,但我不知道如何才能工作 我试图做的是在我的分部类下创建一个Dim,在gridview.row命令中设置值,然后在稍后的按钮上尝试获取值。单击 Partial Class CustNSSF_MinSide Inherits System.Web.UI.Page Dim vr As New vrClass Dim _ActNo As String
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
变量将在每次
回发时清除。您需要将其存储在视图状态
或会话
中。您可以这样做:
Property _ActNo As String
Get
Return ViewState("_ActNo")
End Get
Set(ByVal value As String)
ViewState("_ActNo") = value
End Set
End Property
该值将消失,因为所有变量(或控件)都将在每个页面的生命周期结束时处理GV\u Relations\u row命令
仅在row命令
时触发,而btn\u lagre\u单击
则是另一种操作。您可以将此值存储在会话
变量、视图状态
中,或者(如您所做)存储在隐藏字段
中。因此,当用户单击btn\u lagre
时,导致RowCommand
的上一个操作是不同的回发,因此变量为Nothing
因此,一种方法(除了您的HiddenField
aproach)是使用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
然后您可以通过以下方式进行设置:
Me.ActSeqNo = System.Int32.Parse(GV_Relations.DataKeys(index)("ActSeqNo")))
我的老板不喜欢Session,我觉得HiddenField可能比ViewState简单。但至少我知道它为什么不起作用,我想这就是我真正需要的。为我将来节省时间:)