刷新网页后,限制重置在类中声明的变量 [英] Restrict the reset of variable declared in class after webpage refresh

查看:61
本文介绍了刷新网页后,限制重置在类中声明的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello!!!

I have declared a variable under a class of my VB.NET 

<pre lang="vb">
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Private flg_edit As Integer


每当刷新网页时,即使我在后续代码中将其设置为1,该变量(flg_edit)也会重置为0.

我想在整个网页中使用此变量.
我该怎么办?

谢谢!!!

Ernest.


Whenever the webpage is refreshed this variable (flg_edit) is reset to 0 even if I had set it to 1 in the subsiquent code.

I want to use this variable throughout the webpage.
How do I do that?

Thanks!!!

Ernest.

推荐答案

您需要了解的是,每次刷新页面时,都会实例化该页面的新实例.现在,您可能想知道页面中已更改的项目如何设置为其更改的值.

要理解这一点(在经典的ASP.NET中),重要的是要意识到两点:首先是ViewState用于存储更改后的值,第二件事是要知道这些值用于重新填充相关的源(这是一种无用的方式,说您不需要重新绑定大多数数据源,因为绑定的数据已自动为您写入ViewState).

那么,这对您真正意味着什么?好吧,这意味着您可以将所需的值存储在ViewState中,并在ViewState重新填充页面时重新填充它.或者您可以使用Session上下文来存储值,但是考虑到它仅与这一页相关,那就太过分了.

基本上,您只需在代码中执行以下操作:
What you have to understand is that each time you refresh the page, a new instance of that page is instantiated. Now, you may be wondering how items that have been changed in the page are set to their changed value.

To understand this (in classic ASP.NET), it''s important to be aware of two things; the first is that ViewState is used to store changed values, and the second thing is to be aware that these values are used to repopulate the relevant sources (this is a high-faluting way of saying that you don''t need to rebind most datasources because the bound data has been written to the ViewState for you automatically).

So, what does this really mean for you? Well, this means that you can store the value you want in the ViewState, and repopulate it when the ViewState is repopulating your page; or you could use the Session context to store the value, but considering that it''s only relevant on this one page, that would be overkill.

Basically, you could simply do this in your code:
If ViewState["EditNum"] Is Nothing Then
  flg_edit = 1
Else
  flg_edit = CType(ViewState["EditName"], Int32)
End If

并且您以如下方式保存ViewState:

And you save your ViewState like this:

ViewState["EditNum"] = flg_edit


你好!!!

我找到了解决上述困难的方法,如下所示-

只需将变量声明为共享即可,这样就可以在当前页面中使用它而无需重置它.

这样做刷新将不会创建变量的新实例.
Hello!!!

I found a solution to my above difficulty as follows -

Just make the varible declared as shared so that it can be used throughout the current page without it been reset.

Doing this the refresh will not create a new instance of the variable.
Dim Shared flg_edit As Integer


专门检查解决方案2.


这篇关于刷新网页后,限制重置在类中声明的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆