Why dynamically created user controls disappear when controls are not doing full postbacks?

后端 未结 2 2046
执念已碎
执念已碎 2021-01-06 22:20

Based on my current understandings, when you have an UpdatePanel control there is no full postback. Therefore if I dynamically add custom user controls and they have UpdateP

相关标签:
2条回答
  • 2021-01-06 23:10

    OK based on my current understandings, when you have an UpdatePanel control there is no full postback.

    Postbacks triggered from UpdatePanels always execute the full page life-cycle. All the events are triggered normally. It makes no difference whether you use an UpdatePanel or not. Every time you add a control programmatically you need to re-add it on every postback.

    Read this post, it may help you understand a bit better what's going on here.

    0 讨论(0)
  • 2021-01-06 23:15

    By End of the Page Life Cycle all the controls generated at Runtime/Compile Time will be Disposed.

    Below are the Page Events. Please set the BreakPoint on each Event and you can figure out that on each Asynchronous/Synchronous Request, all these Page Events are being executed.

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
    }
    
    protected override void OnPreLoad(EventArgs e)
    {
        base.OnPreLoad(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    protected override void OnLoadComplete(EventArgs e)
    {
        base.OnLoadComplete(e);
    }
    
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
    }
    
    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
    }
    
    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
    }
    

    You need to populate them on each Page Init event in order to persist Viewstate, also events for controls added inside an update panel during button clicks do not seems to get registered until the next Postback. My suggestion is to keep a list of what you have added dynamically, and store it in a session variable

    0 讨论(0)
提交回复
热议问题