2013-09-28 125 views
1

我試圖想出一個功能,它將允許用戶分別按下按鈕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變量的值將再次分配給最大長度。

有沒有什麼辦法可以避免重新分配這個變量並使它只發生一次?

+0

不確定是否瞭解您的問題,但[Page.IsPBackBack](http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx) – Steve

+0

問題是每次當我按下以太按鈕或者下一行時,'determinator = CounterID'將決定器的值賦給數組的長度。問題是如何避免它? – meks

+0

例如,如果數組有6個元素,那麼測定器將等於6.當我按下下一個按鈕時,它將從它的「determinator = determinator-1」中扣除1,但是然後頁面重新加載確定器的值又是6,因爲這個「 determinator = CounterID'再次發生。所以我可以只返回數組中的一個元素 – meks

回答

1

您可以使用一個會話變量,像這樣:

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

你可能想這取決於的IsPostBack執行。

+0

非常感謝。其實際工作。解決方案非常簡單。再次引誘你。 – meks

相關問題