动态创建控件和ASP.NET页面的生命周期 [英] Dynamically created controls and the ASP.NET page lifecycle

查看:156
本文介绍了动态创建控件和ASP.NET页面的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,其中绝大多数的形式在运行时动态生成的(形式定义存储在数据库的可定制)ASP.NET项目。因此,我必须动态地创建和添加我的控件的页每一次的OnLoad火灾,不管的IsPostBack的。这一直工作得很好,.NET需要管理的ViewState这些控件的照顾。

I'm working on an ASP.NET project in which the vast majority of the forms are generated dynamically at run time (form definitions are stored in a DB for customizability). Therefore, I have to dynamically create and add my controls to the Page every time OnLoad fires, regardless of IsPostBack. This has been working just fine and .NET takes care of managing ViewState for these controls.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    RenderDynamicControls()
}

private void RenderDynamicControls()
{
    //1. call service layer to retrieve form definition
    //2. create and add controls to page container
}

我如果用户点击一个给定的按钮(此按钮在设计时创建)的页面应该被重新呈现在一个稍微不同的方式,其中的新要求。因此,除了在code,在执行的OnLoad(即RenderDynamicControls()),我有这样的code:

I have a new requirement in which if a user clicks on a given button (this button is created at design time) the page should be re-rendered in a slightly different way. So in addition to the code that executes in OnLoad (i.e. RenderDynamicControls()), I have this code:

protected void MyButton_Click(object sender, EventArgs e)
{
    RenderDynamicControlsALittleDifferently() 
}

private void RenderDynamicControlsALittleDifferently()
{
    //1. clear all controls from the page container added in RenderDynamicControls()

    //2. call service layer to retrieve form definition

    //3. create and add controls to page container
}

我的问题是,这是真的来完成我后的唯一途径?这似乎超出了哈克有效地渲染表单只是两次到一个按钮点击响应。我是从我的研究收集,这仅仅是在页面生命周期在ASP.NET是如何工作的:即,被调用子事件之前的OnLoad射击必须在每个回发。不过,这是值得有喝库尔援助之前与SO社区进行检查。

My question is, is this really the only way to accomplish what I'm after? It seems beyond hacky to effectively render the form twice simply to respond to a button click. I gather from my research that this is simply how the page-lifecycle works in ASP.NET: Namely, that OnLoad must fire on every Postback before child events are invoked. Still, it's worthwhile to check with the SO community before having to drink the kool-aid.

在一个相关的说明,一旦我得到这个功能完成后,我打算在页面上抛出一个UpdatePanel通过Ajax进行页面更新。任何code /建议,使过渡更容易将远AP preciated。

On a related note, once I get this feature completed, I'm planning on throwing an UpdatePanel on the page to perform the page updates via Ajax. Any code/advice that make that transition easier would be much appreciated.

推荐答案

从德克德克: - )

你是什么意思与RenderDynamicControls?创建和设置的控制?如果这是你的意图不是ASP.NET是管理你的ViewState的,但你怎么做。如果您填写的每负荷控制,你总是覆盖现有的ViewState!

What do you mean with RenderDynamicControls? Create and set controls? If this is your intention not ASP.NET is managing your ViewState, but you do. If you fill the controls on every load, you always overwrite the existing ViewState!

如果你想使用ViewState中,在页面初始化时创建的控制和负载情况下填补他们,但前提是请求不是回发。这是必要的,因为ASP.NET再现init和负载之间的视图状态。而这也是您所描述的两个渲染循环的原因。您需要首先创建控件周期,因为ASP.NET不能没有一个正确的控制集恢复的ViewState和ASP.NET不能没有它你的回答正确的反应。

If you want to use the ViewState, create your controls in the pages init event and fill them in the load event, but only if the request isn’t a postback. This is necessary, because ASP.NET recreates the ViewState between init and load. And this is also the reason for the two "rendering cycles" you describe. You need the first control creation cycle because ASP.NET can’t restore the ViewState without a proper control set and ASP.NET can’t react proper on your response without it.

返回到code:一般你RenderDynamicControlsALittleDifferently是行不通的 - 因为你创建你的控件在页面生命周期为时已晚,你会通过插入新的对象来控制收集损坏的ViewState。在一个类似的情况下,我通过变向解决了这个问题的页本身(的Response.Redirect)。在这种情况下RenderDynamicControls会做这项工作的基础上,有点不同的情况:你改变你的内部状态后。

Back to your code: In general your RenderDynamicControlsALittleDifferently wouldn’t work - because you create your controls too late in the pages life cycle and you would damage the ViewState by inserting new objects to the control collection. In a similar situation I solved this problem by a redirecting the page to itself (Response.Redirect). In this case RenderDynamicControls would do the job, based on a "little differently situation" after you change your internal state.

这篇关于动态创建控件和ASP.NET页面的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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