我正在使用 ASP.NET 和 Visual Basic 制作自定義表單,用于填寫資訊。一旦我單擊一個按鈕,就會呼叫一個函式,該函式從該表單中獲取位資訊,將它們放入一個物件中,然后將該物件添加到串列中。這被用于一種排隊輸入系統,因此表單將被多次編輯和提交。
出于某種原因,它沒有將新物件添加到串列的下一個索引位置,而是替換了 0 處的任何內容。因此,當時串列中只有一個物件。
這是自定義表單:

這是我的自定義物件,它當前位于我的 _Default 類之上:
Public Class QueueItem
Public Property _TestName As String
Public Property _ValueID As String
Public Property _MathOperator As String
Public Property _ValueInput As Integer
Public Sub New()
End Sub
Public Sub New(ByVal TestName As String, ByVal ValueID As String, ByVal MathOperator As String, ByVal ValueInput As Integer)
_TestName = TestName
_ValueID = ValueID
_MathOperator = MathOperator
_ValueInput = ValueInput
End Sub
End Class
該串列在我的 Page_Load 函式上方、_Default 類中宣告,并且是公開的。這是串列定義:
Public QueueList As List(Of QueueItem) = New List(Of QueueItem)()
而且,這是按下“添加到佇列”按鈕時呼叫的內容:
Protected Sub AddToQueueButton_Click(sender As Object, e As EventArgs) Handles AddToQueueButton.Click
'Adds a new QueueItem to the QueueList
'Values pulled from the dropdown lists in the custom form
QueueList.Add(New QueueItem() With {
._TestName = TestName.SelectedValue,
._ValueID = ValueID.SelectedValue,
._MathOperator = MathOperator.SelectedValue,
._ValueInput = ValueInput.Text
})
'Below section is for testing
Dim test1 As String = QueueList(0)._TestName
Dim test2 As String = QueueList(0)._ValueID
Dim test3 As String = QueueList(0)._MathOperator
Dim test4 As String = QueueList(0)._ValueInput
Dim testmessage As String = test1 " | " test2 " | " test3 " | " test4
Dim count = QueueList.Count
Dim capacity = QueueList.Capacity
Response.Write("<script language='javascript'>alert('" testmessage "');</script>")
End Sub
所以,正如你所看到的,我有一些測驗變數和東西,我用它們來確保它正常作業。每次呼叫時,都會將一個物件添加到串列中,我查看串列的計數和容量,并在警報中顯示所有資訊。
警報的此資訊始終從索引 0 讀取。因此,將資訊添加到串列的次數應該無關緊要,0 應該保持不變,并且物件應該添加到 1,然后是 2,依此類推。對?
好吧,每當我提交新資訊時,0 都會發生變化,而且數量或容量都不會增加到第一個條目之后。它們總是顯示為好像串列中只有一項。
這是我兩次運行佇列條目表單,最后有兩個不同的數字:
第一次運行:

Second run:

Since I'm always reading from index 0, that number at the end shouldn't change. It should be giving me the number that's associated with the object at index 0. And, the List.Add function should make the count and capacity go up. But, none of that happens. Instead, it seems to be replacing what was at 0.
If anyone has any tips on how to fix this or can clue me in on what might be going on, that would be greatly appreciated.
uj5u.com熱心網友回復:
每次向您的頁面發出請求時,都會創建類的一個新實體來處理該請求。對于回發請求,可能會加載一些資料 - 例如,發布到服務器的欄位值,以及存盤在ViewState. 但是存盤在頁面欄位中的資料不會跨請求持久化。
代碼不會替換index 處的專案0;它正在將一個專案添加到一個空串列中。
您需要以某種方式在請求之間保留您的串列。您可以將其存盤Session在ViewState.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361783.html
