I want to prepend an aspx page with another page at runtime. I know the better solution would be to use a usercontrol, But for that I would have to put all the elements of my aspx page in a user control & do that for several pages. Isnt there a way to load an aspx page inside another page like a user control? Also I would want to make all the fields readonly of the appended page. Is there a way I can do this?
If usercontrol is the only way, then I have a query. When I loaded my userControl in my PageLoad event, I cannot cast the control to my usercontrol name, & because of that I cannot set a property of my control. I want to do this, but this gives error at compile time:
MyControl myUserControl =(MyControl)Page.LoadControl("~/Controls/MessageControl.ascx");
Here MyControl is not available (It is not recognizable by the compiler) I tried Ctrl+Dot on MyControl, the compiler is not adding any reference to it also, It gives the following error
The type or namespace name 'MyControl' could not be found (are you missing a using directive or an assembly reference?)
When I do this, it works:
var ctrl = LoadControl("~/Controls/MessageControl.ascx");
MyPlaceholder.Controls.Add(ctrl);
But now ctrl is not casted which prevents me to set a property of my user control.
Also does it matter If I instantiate the control in Page_Init instead of Page_load? Because in Page_Init I am unable to make a DB call, probably the assemblies/db config hasnt loaded in Page_Init..