ASP.NET控件添加回发 [英] ASP.NET Add Control on postback

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

问题描述

我已经把一个简单的表格,突出动态表单的概念。我需要做的就是将控件添加到页面,当用户点击添加按钮。

我有一个存储的控制产生的量,点击按钮时被递增的那一刻简单的计数器。

起初我以为它会和调用RecreateChildControls(类自CompositeControl继承)的事件处理程序一样简单。这确实创建基于递增的值的新的控制,但所有的控制状态会丢失。我猜想这是因为事件已经初始化和放大器之后被解雇;装载阶段。

是否有任何其他方式做到这一点?我可以得到它的初始化事件检查回发值继续工作,然而这似乎有点哈克。


解决方案

  

这确实创建基于增加值的新的控制,但所有的控制状态会丢失。


您正在调用该函数在页面生命周期。国家应用到你的控件加载的阶段,所以如果不创建控件的的那个阶段,国家将无法恢复,因为当它试图不存在的控件应用的状态。

您需要在页面的Init事件,以创建控件。

就个人而言,我不是在ASP.Net动态控制风扇。他们有自己的位置,但更多的时候,我选择允许控制合适的最大数量,把他们的所有的网页上最初,只有启用/禁用/隐藏/显示他们需要。

I've put together a simple form to highlight the concepts of dynamic forms. What I need to do is add a control to the page when the user clicks the "Add" button.

I have a simple counter at the moment that stores the amount of controls created, which is incremented when the button is clicked.

At first I thought it would be as simple as calling RecreateChildControls (the class inherits from CompositeControl) on the event handler. This does create the new controls based on the incremented value, but all the control state is lost. I'm assuming this is because the event has been fired after the Init & Load phase.

Is there any other way to do this? I can get it to work by inspecting the postback value on the Init event, however this seems to be a little hacky.

解决方案

This does create the new controls based on the incremented value, but all the control state is lost.

You're calling the function too late in the page life cycle. State is applied to your controls for the "Load" stage, and so if the controls are not created before that stage the state won't be restored, because the controls don't exist when it tries to apply the state.

You need to create the controls in the Page's Init event.

Personally, I'm not a fan of dynamic controls in ASP.Net. They have their place, but more often I choose a suitable maximum number of allowed controls, put them all on the page initially, and only enable/disable/hide/show them as needed.

这篇关于ASP.NET控件添加回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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