是否有可能设置code背后asp.net控件属性值不增加它的ViewState? [英] Is it possible to set asp.net control property values in code behind without adding it to viewstate?

查看:133
本文介绍了是否有可能设置code背后asp.net控件属性值不增加它的ViewState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的code值分配给服务器控件的属性后面初始化的形式。我并不需要/希望被添加到视图状态这些值。 (我知道我可以为特定的控制禁用视图状态,但我需要ViewState的一些值可能会改变,需要回送过程中依然存在。)

I want to assign values to server control properties in my code behind to "initialize" a form. I don't need/want these values to be added to the viewstate. (I know I can disable viewstate for a specific control, but I need viewstate as some values may change and need to be persisted during postbacks.)

目前对于我来说,似乎它无法加载code这些值,而无需将它们添加到视图状态。

At the moment for me it seems its not possible to load these values in code without having them added to the viewstate.

据我了解会发生以下情况:

As I understand the following happens:

页:preINIT

我能值添加到一些控件(它的工作,例如用文字控制设置文本值),在这里,但因为控制的init方法执行后,我无法如发现在这个阶段RegisterUser控件及其子控件还,他们仍然为空。 >因此,在这种特殊情况下无解,还有更多的情况下

I could add values to SOME controls (its working for example with a literal control to set the text value) here BUT since control's init methods are executed later I cannot for example find a RegisterUser control and its child controls at this stage yet, they are still null. > so no solution in this specific case, there's more cases

控制:初始化

我无法从我的页面中达到这一点,这只能在用户控制code例如里面,当你写你自己的用户控件,并希望初始化它的价值时,我ofcourse不想要创建自己的控制每个控制我需要先执行一些逻辑来初始化。

I cannot reach this point from within my page, this can only be used inside the user control code for example when you write your own usercontrol and want to initialize its values, I ofcourse dont want to create my own control for each control I need to initialize with executing some logic first.

控制:TrackViewState

Control: TrackViewState

启用视图状态跟踪等等东西从这里后,我上设置将会被标记为脏,因此加入到视图状态。
  页:初始化
  页:TrackViewState

Viewstate Tracking is enabled so from here on after anything I set will be marked as 'dirty' and therefore added to the viewstate. Page:Init Page:TrackViewState

我也是说得:

在code的后面,没有禁用控制ViewState属性。

In code behind, without disabling a controls viewstate property..


  • 您可以初始化1级,从在preIinit方法页面控件的子控件的属性,而不必被添加到视图状态的值。

  • ,而无需添加到视图状态的值不能初始化从于页的1级子控件控制子控件的属性。

然后,


  • 您可以声明通过使用资源初始化控制的属性,但是这并不能规避,你需要执行逻辑场景

  • 您可以通过使用数据绑定语法执行逻辑,但你必须手动调用在数据绑定控件有同样的问题如上

请问有没有办法来迭代一组控制/子控件,并启动在code值背后withouth的这些值添加到视图状态?有

Is there no way to iterate a set of controls/child controls and initiate values in code behind withouth having these values added to the viewstate?

推荐答案

通过这里没有反应和一些进一步的研究,我现在在点到conlude,你确实可以不重复一组控制/ childcontrols并在启动值code的后面,而无需添加到视图状态这些值,因为在问题中提到的原因。

With no reactions here and some further research, I am now at the point to conlude that you can indeed NOT iterate a set of controls/childcontrols and initiate values in code behind without having these values added to the viewstate, because of reasons mentioned in the question.

有一些部分的解决方案却清楚这里所作:的http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

There are some partial solutions however as made clear here: http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

一种可能性是挂接到控件本身的init事件声明,你就必须做到这一点每个控件。在code后面,在事件处理中可以设置属性和因为ViewState是不跟踪但它不会被保存在ViewState中。

One possibility is to hook into the init event of the control itself declaratively, you would have to do that for each control. In code behind, inside the event handler you can set the property and since viewstate is not tracking yet it will not be saved in viewstate.

有不同的场景的更多的选择,例如通过重写CreateChildControls方法初始化自定义控件内动态添加子控件。绝对值得看完上面提到的'真正了解视图状态链接。

There are more options for different scenario's for example initializing dynamically added child controls inside a custom control by overriding the CreateChildControls method. Definately worth reading the above mentioned 'Truly Understanding Viewstate' link.

这篇关于是否有可能设置code背后asp.net控件属性值不增加它的ViewState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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