【问题标题】:How to avoid reassignment of a variable on page load如何避免在页面加载时重新分配变量
【发布时间】:2013-09-28 13:54:55
【问题描述】:

我正在尝试提出一个功能,该功能允许用户分别按 BACK 和 NEXT 按钮更改数据。

我正在使用此代码:

         CounterID = IdArray.Length 'array of IDs

        determinator = CounterID 'determining which index is active and putting it on the top of array

        If Ident = 1 Then ' if button back is pressed
            determinator = determinator + 1
            If determinator <= CounterID Then
                'some actions
            End If


        Else
            determinator = determinator - 1
            If determinator >= 0 Then
                'some actions
            End If
        End If

它确实有效。但部分。我的问题是,每当按下按钮时,DETERMINATOR 变量的值都会再次分配给最大长度。

有什么办法可以避免重新分配这个变量并让它只发生一次?

【问题讨论】:

  • 不确定是否理解您的问题,但Page.IsPostBack
  • 问题是每次我按下以太按钮后退或下一行determinator = CounterID 将确定器的值分配给数组的长度。问题是如何避免?
  • 例如,如果数组有 6 个元素,则确定器将等于 6。当我按下按钮下一步时,它将从中减去 1 determinator = determinator - 1,但随后页面重新加载,确定器的值再次为 6因为这determinator = CounterID 再次发生。所以我只能返回数组的一个元素
  • 您是否尝试仅分配determinator=CounterID if Page.IsPostBack=False
  • 是的,我试过了,它根本没有任何东西

标签: asp.net vb.net webforms


【解决方案1】:

您可以使用会话变量,如下所示:

If Session("determinator") <> Nothing Then 
  determinator = Session("determinator")
else
  CounterID = IdArray.Length 'array of IDs
  determinator = CounterID
  Session("determinator") = determinator
end if

您可能希望根据 IsPostBack 执行此操作。

【讨论】:

  • 非常感谢。它实际上工作。解决方案非常简单。再次感谢你。
猜你喜欢
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 2020-02-12
  • 2018-01-06
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多