1
Public Class index
Inherits System.Web.UI.Page
Dim arr(9) As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
arr(0) = 23
End Sub

Protected Sub bntInsert_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bntInsert.Click
arr(0) = 999
End Sub   

Protected Sub bntShow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bntShow.Click
txtid.Text = arr(0).ToString
End Sub
End Class

结果是当我单击 bntInsert 并在 bntShow 之后它仍然在 txtid 中显示值“23”。请帮我!

4

2 回答 2

0

1:asp.net 是无状态的。2:检查回发事件?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
 if page.isPostBack then
arr(0) = 23
end if
End Sub
于 2013-02-21T09:32:07.057 回答
0

这是因为每个按钮单击最终都会先调用 page_load。我猜您正在尝试使用公共变量来保留值,但您需要了解特定变量仅对特定请求有效。对于每个后续请求,包括按钮点击(这是回发),该变量都将丢失。

您可能希望将 arr 保留在会话变量中以在往返之间保存其值。

于 2013-02-21T09:45:22.233 回答