如何在运行时为asp.net中的公共变量赋值?

时间:2013-02-21 09:24:33

标签: asp.net

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”。请帮帮我!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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