简体   繁体   English

如何维护变量VB.NET的值

[英]How to maintain the value of a variable VB.NET

I am a beginner developer .. I need help .. please see the following example .. sorry for poor english ... 我是初学者。.我需要帮助..请参阅以下示例..对不起,英语不好...

I have a string variable called str assigned with "John". 我有一个名为str的字符串变量,分配给“ John”。 I changed it to "Dave" when the first button is clicked. 单击第一个按钮时,将其更改为“ Dave”。 When the second is clicked, I displayed the value to a label. 单击第二个时,我将值显示在标签上。 I wanna see Dave but I just see John. 我想看戴夫,但我只看约翰。 Why is that? 这是为什么?

There is nothing on page load and nothing else where. 页面加载上没有任何内容,其他地方也没有。 I know I can put into a session but this is on the same page. 我知道我可以参加一个会议,但这是在同一页上。 Can I not do without session or viewstate. 没有会话或视图状态,我不能做吗?

Thanks. 谢谢。

    Partial Class  _Default
        Inherits System.Web.UI.Page

        Private str As String = "John"

        Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
            str = "Dave"        
        End Sub

        Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click    
           Label1.Text = str
        End Sub
    End Class

Welcome to the world of stateless programming; 欢迎来到无状态编程的世界; variables don't survive after a page is rendered; 呈现页面后,变量将无法生存; you need to use the Session object to save them. 您需要使用Session对象保存它们。

In your button click, set Session("PersonName") = "Dave" 在您的按钮中单击,设置Session(“ PersonName”)=“ Dave”

In Page_Load, have code such as: 在Page_Load中,具有以下代码:

str = Session("PersonName").ToString() str = Session(“ PersonName”)。ToString()

I would highly recommend working through a few tutorials in ASP.NET programming to familarize yourself with all the concepts you need. 我强烈建议您阅读ASP.NET编程中的一些教程,以熟悉所需的所有概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM