ASP.NET隐藏字段持久性在生命周期 [英] ASP.NET Hidden Field Persistence During Life Cycle

查看:172
本文介绍了ASP.NET隐藏字段持久性在生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我可以俯瞰页面生命周期这里的基本概念,并已经(或者是因为我无法找出正确的关键字或尚未要求)无法找到现有的应答这样原谅我,如果这已问。

基本上,我需要坚持在客户端和服务器侧之间的可变对象。由于视图状态进行加密/序列化和会话状态是服务器端的唯一,我的解决方案是使用隐藏域 - 很容易的,对不对?那么这里是我的问题......它好像它的工作,但数据不被传播,因为我会一直的预期。

我的期望是这样的:


  1. 页面加载的第一次。服务器端类识别出隐藏字段为空,初始化容器类,序列化类JSON字符串和值写入隐藏字段。

    • Page_Init:不可用

    • 的Page_Load:不可用。

    • Page_LoadComplete:可用。


  2. 服务器处理完成,对象现在是可供客户端code使用。

  3. 在隐藏域中的对象是由客户端code突变。客户端code,则触发回发到服务器(通过一个按钮)。

  4. 服务器端处理开始...

    • Page_Init:不可用

    • 的Page_Load:可用,包括客户端更改

    • Page_LoadComplete:可用,包括客户端更改


  5. 所有是正确的世界,一个双彩虹闪耀着窗外和一个神奇的独角兽给了我一个眼色和点头。

我的观察是这样的:


  1. 页面加载的第一次。服务器端类识别出隐藏字段为空,初始化容器类,序列化类JSON字符串和值写入隐藏字段。

    • Page_Init:不可用。 (如预期)

    • 的Page_Load:不可用。 (如预期)

    • Page_LoadComplete:可用。 (如预期)


  2. 服务器处理完成,对象现在是可供客户端code使用。

  3. 在隐藏域中的对象是由客户端code突变。客户端code,则触发回发到服务器(通过一个按钮)。

  4. 服务器端处理开始...

    • Page_Init:不可用。 (如预期)

    • 的Page_Load:可用,但不能更新,在客户端所做的更改。 (意外)。

    • Page_LoadComplete:可用,包括客户端的变化。 (如预期)


  5. 在我的房间,乌云的形式和我开始思考我的笔记本电脑是否会生存的第二层脱落的阳台上。

结论

这是害我的一对夫妇的原因有点混乱...第一个是,我从来没有使用LoadComplete事件前,似乎无法找到建议有必要甚至可以说,任何的例子它应该做的方式。第二个是由时间负载完全升高,依赖于从客户端的数据的其他事件已经被解雇。

任何帮助/解释/建议;地狱,甚至批评AP preciated!

谢谢,杰森


解决方案

释解结果
您可以更新在JavaScript HiddenField 值,并让他们回到在服务器上。结果
如果你希望你的对象加载,使用 LoadComplete 后可用就可以了。结果
如果你想,当他们加载这个对象是提供给所有的控件,最早你可以从投入是超载 preLOAD 和创建对象存在的数据。结果
没有与你的逻辑没有问题。

结论结果
有一个在你的实现它的一些错误。结果
让我们来看看code现在。

I feel like I may be overlooking a fundamental concept of the page life-cycle here and have been (either because I can't figure out the right keywords or it hasn't been asked) unable to locate an existing answer so forgive me if this has been asked.

Basically, I need to persist a mutable object between the client side and the server side. Since the viewstate is encrypted/serialized and the session state is server-side only, my solution was to use a hidden field--easy enough, right? Well here's my problem... it seems as though it's working but the data isn't being propagated as I would've expected.

My expectation was this:

  1. Page is loaded for the first time. Server-side class recognizes that the hidden field is empty, initializes the container class, serializes the class to a JSON string and writes that value to the hidden field.
    • Page_Init: Unavailable.
    • Page_Load: Unavailable.
    • Page_LoadComplete: Available.
  2. Server processing completes, object is now available for use by client code.
  3. Object in hidden field is mutated by client code. Client code then fires a postback to the server (via a button).
  4. Server-side processing begins...
    • Page_Init: Unavailable.
    • Page_Load: Available, including client-side changes.
    • Page_LoadComplete: Available, including client-side changes.
  5. All is right in the world, a double-rainbow shines outside my window and a magical unicorn gives me a wink and a nod.

My observation is this:

  1. Page is loaded for the first time. Server-side class recognizes that the hidden field is empty, initializes the container class, serializes the class to a JSON string and writes that value to the hidden field.
    • Page_Init: Unavailable. (As expected)
    • Page_Load: Unavailable. (As expected)
    • Page_LoadComplete: Available. (As expected)
  2. Server processing completes, object is now available for use by client code.
  3. Object in hidden field is mutated by client code. Client code then fires a postback to the server (via a button).
  4. Server-side processing begins...
    • Page_Init: Unavailable. (As expected)
    • Page_Load: Available, but not updated with changes made on the client-side. (Unexpected).
    • Page_LoadComplete: Available, including client-side changes. (As expected)
  5. A dark cloud forms over my cubicle and I begin to contemplate whether or not my laptop would survive the second-story fall off the balcony.

Conclusion

This is causing me a bit of confusing for a couple reasons... the first is that I've never used the "LoadComplete" event before and can't seem to find any examples that suggest it's necessary to or even that it should be done that way. The second is that by the time load complete is raised, other events that rely on the data from the client side have already been fired.

Any help/explanation/suggestion; hell, even criticism is appreciated!

Thanks, Jason

解决方案

Explaination
You can update HiddenField values in javascript and get them back at the server.
If you want your object to be available after Load, using LoadComplete is ok.
If you want this object to be available to all controls when they load, the earliest you can get the data from inputs is by overloading PreLoad and creating your object there.
There is no problem with your logic.

Conclusion
There is some bug in your implementation of it.
Lets take a look at the code now.

这篇关于ASP.NET隐藏字段持久性在生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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