如何处理复合自定义控件中放置在可绑定模板上的子控件的视图状态?
我有一个复合数据绑定控件,它托管 IBindableTemplate 并根据某些条件将标记动态加载到控件中。 现在,当这些子控件加载到复合控件中并且存在回发时,我会丢失子控件的视图状态。 有没有办法,我可以在回发时保存子控件的视图状态?
我也参考。 斯科特的解释使用 http://scottonwriting.net/sowblog/posts/2129.aspx; 但没有用。
I've a composite data-bound control which hosts a IBindableTemplate and dynamically loads a mark-up based on some condition into the control. Now, when these child controls are loaded into the composite control and postback is there, I lose viewstate of the child controls. Is there a way, I can save viewstate of the child-controls on the postback?
I also ref. to the Scott's explanation using http://scottonwriting.net/sowblog/posts/2129.aspx; but of no use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有足够的信息。 什么时候创建控件? 什么时候将它们添加到 Controls 集合中? 什么是条件?它会在回发时发生变化吗?
如果在正确的时间添加了控件,则视图状态会在页面周期结束时(回发或不回发)自动保存。
如果您稍后添加控件,在某些情况下是在完成所有初始化之后,那么就为时已晚。
更新
如果没有代码,就很难猜测故障发生在哪里。 让我们检查一个带有自定义模板的中继器,它可以根据某些条件加载控件。 此示例可以正常工作,但如果在 Page_Load 上完成模板分配,则会失败。 这是否与您的情况类似?
形式:
代码:
There is not enough information. When do you create controls? When do you add them to the Controls collection? What is condition and does it change on postback?
The viewstate is saved automatically at the end of the page cycle (postback or not) given that controls are added at the right time.
If you are adding controls later on, in some event after all initialization has been done, then it is too late.
Update
Without code it is difficult to guess where the break down occurs. Let's examine a Repeater with custom template which could load controls base on some condition. This sample is working, but it would fail if the template assignment was done on Page_Load. Is this something similar to your situation?
Form:
Code:
您的理论代码几乎接近我的情况,只有一个主要区别是我没有从 IBindable 扩展我的模板类,而是我在标记本身中提供模板的控件,这是要求。
Scott 还明确表示,如果我们像您一样将控件添加到模板中,
那么视图状态将在每次回发时自动保留。 我所需要的只是允许我的模板在标记中实例化,并且在回发时仍然保留视图状态,并允许我获取控件的状态,即使我曾经使用一些不同的数据重新启动模板。
Your theoretical code is almost near to my situation with only one major difference that I don't extend my template class from IBindable as such rather I provide the controls for the templates in the mark-up itself which is the requirement.
Scott also, makes it clear that if we add the controls to template like you have done
then the view-state will automatically be persisted on every postback. All I need is to allow my templates be instantiated in the mark-up and still persist viewstate on postback and allow me to fetch the status of control even if I used to re-initiate the template with some different data.