简体   繁体   English

object 对 Session 属性的引用

[英]Reference of an object to a Session property

I have a text box in my aspx page, I created a Session property (textbox type), then:我的 aspx 页面中有一个文本框,我创建了一个 Session 属性(文本框类型),然后:

textBoxInSession = myTextBox;

If I change the Text property in textBoxInSession, the Text property in myTextBox does not changed.如果我更改 textBoxInSession 中的 Text 属性,则 myTextBox 中的 Text 属性不会更改。

isn't textBoxInSession a reference to myTextBox? textBoxInSession 不是对 myTextBox 的引用吗?

In general, you should not persist UI elements/controls to the Session or Application state.通常,您不应将 UI 元素/控件保留到 Session 或应用程序 state。 When you store an object instance, you pin it and everything that holds a reference to it in memory.当你存储一个 object 实例时,你将它和所有对它的引用都固定在 memory 中。 Because ASP.NET creates a new instance on each page execution, this can result in considerable amounts of memory being consumed by non-garbage-collectible Page and Control instances, if you make heavy use of this technique.因为 ASP.NET 在每个页面执行时都会创建一个新实例,如果您大量使用此技术,这可能会导致大量 memory 被非垃圾收集的页面和控制实例消耗。

You should store value types, strings and serializable POCOs or business layer types instead, and rebind them to UI controls when you need them.您应该存储值类型、字符串和可序列化 POCO 或业务层类型,并在需要时将它们重新绑定到 UI 控件。

When your page serves a new Request, it's a new instance of the page, containing new instances of your controls.当您的页面提供新请求时,它是页面的新实例,包含您的控件的新实例。 So that TextBox is another instance than what you stored in Session on a previous Request.因此,该 TextBox 是您在上一个请求中存储在 Session 中的另一个实例。

The testBixInSession is a copy of your page text box, not a reference. testBixInSession 是您的页面文本框的副本,而不是参考。

Each request for a page is handled by a different thread, and a new instance of your class is created for that request.对页面的每个请求都由不同的线程处理,并为该请求创建 class 的新实例。 The Textbox stored in Session is the TextBox from the previous instance of your class from the previous page request.存储在 Session 中的文本框是来自上一个页面请求的 class 的上一个实例的文本框。

The behavior you expect will work if it is during the same request.如果在同一请求期间,您期望的行为将起作用。 However that really serves no purpose since you can reference the TextBox directly.但是,这实际上没有任何作用,因为您可以直接引用 TextBox。

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

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