我有一个aspx网站,其中一个数组在名为Private Input(10)的inherits子句之后在页面顶部声明为String,它从Private strCalculation获取其值为String。当我单击存储按钮时,第一个数组值接收strCalculation值,然后数组槽增加1。但是,页面不保留strCalculation,因为我刚刚将减法按钮显示在标签中并且没有值!这是代码,首先发生减法方程,其中strCalculation得到它的值:
Option Strict On
Partial Class Index
Inherits System.Web.UI.Page
Private i As Integer = 0
Private Input(10) As String
Private strCalculation As String
Protected Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
lblMessage.Text = String.Empty
Dim Numfirst As Double
Double.TryParse(txtFirstNum.Text, Numfirst)
Dim Numsecond As Double
Double.TryParse(txtSecondNum.Text, Numsecond)
Dim answer = Convert.ToString(Numfirst - Numsecond)
strCalculation = (Numfirst & " - " & Numsecond & " = " & answer)
txtResults.Text = strCalculation
txtResults.Focus()
End Sub
Protected Sub btnStore_Click(sender As Object, e As EventArgs) Handles btnStore.Click
Input(i) = strCalculation
i += 1
End Sub
答案 0 :(得分:0)
由于页面的生命周期很短(每个请求),您的数组将不会保留。将它保存到会话中:
'Is this the firt input?
If Session("InputCount") Is Nothing OrElse Session("InputCount") = "" Then
Session("InputCount") = "0"
End If
'Set the new value.
Dim intInputCount As Integer = Convert.ToInt32(Session("InputCount")) + 1
Session("Input" + intInputCount.ToString()) = strCalculation
'Increment the input count.
Session("InputCount") = intInputCount.ToString()
然后引用存储的输入:
If Not Session("InputCount") Is Nothing OrElse Session("InputCount") <> "" Then
Dim intRecordCount = Convert.ToInt32(Session("InputCount"))
For intCursor As Integer = 1 To intRecordCount
Dim strCalculation As String = Session("Input" & intCursor.ToString())
'Do Something with the value.
Next
End If