gpt4 book ai didi

c# - 在页面重新加载时,所有对象都被销毁了?

转载 作者:行者123 更新时间:2023-11-30 16:18:28 24 4
gpt4 key购买 nike

如何在 c#/asp.net 中创建一个在页面重新加载/回发后仍然存在的对象 X 的持久列表?这在 c# 中从来都不是问题,但在 asp.net 中,回发将消除一切。

最佳答案

Asp.net 是为 HTTP 构建的协议(protocol)是 stateless ,因此您无法在回发中找到该对象。您可以改用 View 状态。

View 状态

Microsoft® ASP.NET view state, in a nutshell, is the technique used by an ASP.NET Web page to persist changes to the state of a Web Form across postbacks.

View 状态的作用

View state's purpose in life is simple: it's there to persist state across postbacks. (For an ASP.NET Web page, its state is the property values of the controls that make up its control hierarchy.) This begs the question, "What sort of state needs to be persisted?" To answer that question, let's start by looking at what state doesn't need to be persisted across postbacks. Recall that in the instantiation stage of the page life cycle, the control hierarchy is created and those properties that are specified in the declarative syntax are assigned. Since these declarative properties are automatically reassigned on each postback when the control hierarchy is constructed, there's no need to store these property values in the view state.

关于c# - 在页面重新加载时,所有对象都被销毁了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com