asp.net动态添加用户控件节省回发后值 [英] asp.net dynamically added user control saving values after postback

查看:188
本文介绍了asp.net动态添加用户控件节省回发后值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题。我有我想允许用户添加使用按钮点击必要的,因为很多情况下,用户控件(每一个按钮被点击的时候,我想我的用户控制的另一个实例添加到面板)。它工作正常第一次,但每增加后回删除所有添加的控件。我没有问题,跟踪用户的数量控制用户增加了,但我怎么保证他们留在他们的回发前的状态?我读过关于使用与SaveViewState和LoadViewState人一些职位,但我一直没能找到任何的例子。

我最大的问题是确保所有的从每个用户控件中的文本框和dropdownlists保持填充相同的文本/选定值/每篇文章后面的数据后,

在此先感谢,


解决方案

既然你编程方式将控件添加到您的网页,你需要重新创建它们在每次回发。

此外,您重新对 preINIT 初始化页面的情况下以编程方式添加控件必要。这是正确的视图状态恢复事件管理。

如果你不这样做,控制将会消失在回发,他们将不会处理任何事件。

修改

虽然建议添加上动态控制 preINIT 初始化这是真的(为达斯汀·霍奇说: ),如果将它们添加在的Page_Load 它可能工作。我说你应该避免,除非你有没有其他选择。


  

    

您可能能够在加载您的控件脱身
    Page_Load事件处理程序,妥善维护视图状态。结果
    这一切都取决于你是否通过编程设置动态加载控件的所有属性,如果是这样,当你这样做相对
    Controls.Add被(dynamicControl)
    行。结果
    这方面的一个彻底的讨论是有点超出了本文的范围,但它可能工作的原因是因为
    Controls属性的Add()方法递归加载父的视图状态到它的孩子,即使负载视图状态的阶段已经过去了。


  

Here's my issue. I have a usercontrol that I want to allow users to add as many instances of as necessary using a button click (each time a button is clicked, I want to add another instance of my user control to a Panel). It works fine the first time, but each additional post back removes all of the added controls. I have no problem keeping track of the number of user controls a user has added but how do I ensure they stay in the same state they were before the postback? I've read some posts about people using SaveViewState and LoadViewState but I haven't been able to find any examples.

My biggest issue is ensuring that all of the text boxes and dropdownlists from each user control stays populated with the same text/selected value/data after each post back

Thanks in advance, Ben

解决方案

Since you're programmatically adding controls to your page, you'll need to recreate them on EACH postback.

Also, it's necessary that you recreate programmatically added controls on PreInit or Init event of the page. This is for proper viewstate restoring event management.

If you don't do this, control will be gone on postback and they won't handle any event.

EDIT

Although is recommended to add dynamically controls on PreInit or Init it's true (as Dustin Hodges says) that it may work if you add them on page_load. I'd say you should avoid it unless you have no other option.

You may be able to get away with loading your controls in the Page_Load event handler and maintaining the view state properly.
It all depends on whether or not you are setting any properties of the dynamically loaded controls programmatically and, if so, when you're doing it relative to the Controls.Add(dynamicControl) line.
A thorough discussion of this is a bit beyond the scope of this article, but the reason it may work is because the Controls property's Add() method recursively loads the parent's view state into its children, even though the load view state stage has passed.

Source MSDN

这篇关于asp.net动态添加用户控件节省回发后值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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