Reputation: 5242
I'm trying to set a property on a custom web control before the control's CreateChildControls method is called. The reason being, a lot of logic is performed in there that depends on the value of the property in question. I can set the property directly with an explicit value in HTML and it is picked up in time, but that's no use to me because it needs to be based on a server-side variable.
The tricky bit is, that the EnsureChildControls method is being called from the getter of a "CONTENTS" property of the control which corresponds to tags within the ASPX file. e.g.;
<myControl>
<content>
...
</content>
</myControl>
As far as I know, this in turn triggers the overridden CreateChildControls method, and the logic is executed without the correct value for the property I'm trying to set.
What I'm trying to work out is, where in the ASP.NET life-cycle of my page can I set my property so that it is set before .NET accesses the getter of the "CONTENT" property of of my control?
I've tried OnPreInit on the parent page, but that still is being hit AFTER CreateChildControls on the control. I also don't want to alter the control itself, because it's a generic control and I don't want to special case it for this particular case.
Anyone got any ideas where I can set my property value so it's picked up in the order I'm trying to achieve?
Upvotes: 1
Views: 1084
Reputation: 6784
From my testing it is not possible without modifying the web control in the situation. When a web form is parsed all controls defined in the html are created during the TemplateControl.FrameworkInitialize method, this includes calling the get accessor of any inner properties. Unfortunately FrameworkInitialize is called during ProcessRequest, which is prior to ProcessRequestMain which controls the remainder of the page life cycle.
You can override FrameworkInitialize, but would only get access to the web control before it has been created or after it is too late to set the property.
I have always found this image very useful when trying to decipher the page life cycle.
Upvotes: 3