Property null after postback - Dynamically loaded control

前端 未结 3 1595
北恋
北恋 2020-12-10 17:25

I\'m aware this question has been asked many times before but I suspect I have a unique scenario.

I\'m loading a Child Control (ASCX) and setting a Property on that

3条回答
  •  清歌不尽
    2020-12-10 18:14

    In Microsoft explanations about ASP.NET page life cycle, it is written that dynamically created controls must be created in PreInit.

    It worked for me. Here is my main page :

    protected global::System.Web.UI.HtmlControls.HtmlGenericControl FiltersZone;
    

    (. . .)

     protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            FiltersZone.Controls.Add(new PlanningFiltersSurgeonWeb());
        }
    

    This dynamically created ".ascx" control contains an hidden field :

    
    

    I am now able to retrieve its value from within dynamically created ASCX control Page_Load event, after a "submit" or a "__dopostback('hidTxtPaint')" initiated from JavaScript.

    On the other hand, the hidden field's value is always empty after a POST if its parent ".ascx" control is added in main page's Page_Load event.

提交回复
热议问题