I have a user control that uses the standard if(!IsPostBack){//initialize myself}
paradigm to avoid re-doing initialization during postbacks (so, trading fewer DB hits for increased ViewState usage). That approach serves me well most of the time but there's one place where I want to add this control to the control hierarchy 'late', during a postback.
This, of course, causes the initialization logic to fail, and the control to be rendered in an uninitialized state.
What guard should I be using to determine whether I should initialize, since !IsPostBack
isn't cutting it? I could set a flag during LoadViewState
, but that seems a bit hackish. What I'd like to find is some condition that only happens when a control is first added to the control hierarchy, and key on that. Does such a condition exist?
[edit] Sample pseudocode follows for the containing page:
protected void Page_Prerender(object sender, EventArgs e)
{
Controls.Add(LoadControl("some_control.ascx"));
}
Is there a way for some_control
to know it's been added late?