在这个程序中我创建了一个数组,而点击get按钮则没有显示任何输出,因为str变量是空的。我该如何获得正确的输出...
Partial Class dynamic_array
Inherits System.Web.UI.Page
Dim s(2) As String
Dim str As String
Protected Sub btn_save_Click(sender As Object, e As System.EventArgs) Handles btn_save.Click
Dim i As Integer = 0
For i = 0 To s.Length - 1
s(i) = InputBox("enter name " + (i + 1).ToString)
Next
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
Dim i As Integer = 0
str = ""
For i = 0 To s.Length - 1
str &= s(i) + vbCrLf
Next
MsgBox(str)
End Sub
End Class
答案 0 :(得分:0)
在Asp.Net中,页面级别上设置的所有变量都会在每次回发时重置。这就是为什么在按钮点击时s变量总是空的。
您可以将此信息保存在viewstate
中Viewstate("MyData") = "Something"
我鼓励您熟悉Asp.Net网络表单基本状态管理:http://msdn.microsoft.com/en-us/library/50x35554(v=vs.80).aspx
答案 1 :(得分:0)
您可以使用会话或 ViewState 来存储数组值。
试试这个
Partial Class dynamic_array
Inherits System.Web.UI.Page
Dim s(2) As String
Dim str As String
Protected Sub btn_save_Click(sender As Object, e As System.EventArgs) Handles btn_save.Click
Dim i As Integer = 0
For i = 0 To s.Length - 1
s(i) = InputBox("enter name " + (i + 1).ToString)
Next
ViewState("Array")=s
End Sub
Protected Sub btn_get_Click(sender As Object, e As System.EventArgs) Handles btn_get.Click
Dim i As Integer = 0
str = ""
s= ViewState("Array")
For i = 0 To s.Length - 1
str &= s(i) + vbCrLf
Next
MsgBox(str)
End Sub
End Class
希望这会有所帮助,如果有帮助,马克会很有用。